bibiki 18 Posting Whiz

hey there,
I need an idea on how to store some information in a database.

Let us assume that I want to build an application that allows a user to record what bars he visited each day.

The number of bars is variable, and a user typically visits more than one bar every day.

Now, my first idea is that I should build a comma-separated string out of the id's of each bar, and store it on a single column on a hypothetical table named barsVisited.

Is there perhaps some better way how to do this, or should I just proceed with this idea?

thank you in advance.

bibiki 18 Posting Whiz

he didn't include a question mark, but he did ask a question.
he wants to make his app capable of adding (or performing some other operation) a number to the existing result... anyways.

suchanewb,
you see, when you have such a calculator the equal sign, square root, and squaring lead directly to the result and output it on screen.

the other possible tasks that your calculator should be capable of performing are the arithmetic operations.

if the user clicks the equal sign, square root, or square button, I mentioned what needs to be done, else, he must have clicked an arithmetic operation button on your calculator. in that case, your app should perform the operation with operands being the current result (stored on some variable in your app) and the user input. since inputting a number is the natural followup when the user clicks on a button that represents an arithmetic operation, then it would be equally natural to clean the text input part of your calculator every time such a button is clicked and refresh the current result by performing the previous operation in case it has not been performed (you know it was performed if equal, square root, or square button was clicked last)...

bibiki 18 Posting Whiz

flash builder is really easy. adobe has tutorials on its site for how to use flash builder/flex... as far as styling goes, you can use css to style your application in flash builder too.

bibiki 18 Posting Whiz

msvinaykumar,
with minor modifications. pretty much works. thank you man.

bibiki 18 Posting Whiz

i guess you pass new Graphics g to it or something. do you pass the same g that you set color to on to paint()?

bibiki 18 Posting Whiz

Hey Ricky116,
this is what I got right now:

[+|-]?[0-9]*[.]?[0-9]*[x]?[\\\\\\^]?[0-9]*[.]?[0-9]*

however, this has a loop because it allows to input 123^3 when a user should actually input 123x^3. I know I can add another if and add the x if no x is there, but can you make that regular expression any better man? It is supposed to test strings to see if they can be monomials.

bibiki 18 Posting Whiz

have you by any chance included a super(g) somewhere in either your constructor or paintComponent or repaint() method? if yes, remove it.

bibiki 18 Posting Whiz

hey there,
I am trying to test strings using regex to make sure that the string may represent a monomial.

that is, it must be 123123213x^213123.

I am having difficulties to find any helpful information on how to test for ^.

I am trying this: [+|-][0-9]*[.][0-9]*[x\^]? but I get a complaint that ^ is not a valid escape sequence (I am using eclipse).

in case you wonder what that [.] is, it is to make sure doubles are matched for.

anyone any idea please.

thank you.

bibiki 18 Posting Whiz

Alright NormR1,
I'm good with the code right now. thank you anyways. see you later.

bibiki 18 Posting Whiz

NormR1,
this was my problem:

I have a panel CreateQuestion, another CreateCoourse. Now, inside each panel I have a main method where I build a frame and put the panel on it. However, on the main method inside CreateCourse I also added CreateQuestion onto the frame.

Now, when I added CreateCourse on a frame from a third class, I could not see CreateQuestion content... so, what happened is that I confused myself. Soon after I posted I saw what I was doing wrong. anyways, thank you for your help.

Since we're at it though, I have a question:

I want to make a method that makes sure my database and expected tables inside it exist before my application can be used. However, what I am having difficulties with is determining where to invoke that method.

I have this class, ConnectionHelper that does nothing but constructs a connection to my database and returns it through its ConnectionDB method. perhaps this is where I could invoke the method with a "CREATE IF NOT EXISTS..." for each table that I want in my database and an "INSERT INTO users (username, password) VALUES ('admin', 'admin')"?! any idea? Does this sound good to you?

bibiki 18 Posting Whiz

Hey there,
I have already built my system to log in but I am having difficulties in proceeding with the new panel I want to show after log in is successful.

This is how I'm doing it:

I have built different panels for each use case of my app. So I have a panel that says LoginPanel.

I take username and password in correctly, and everything... Now, I want that when username and password are correct the LoginPanel should be removed, and the my JTabbedPane to be added onto the frame.

I tried:

frame.getContentPane().removeAll();
//frame.getContentPane().add(new MyJTabbedPane());
frame.getContentPane().add(new OneOfMyOtherPanels());
frame.validate();

But this is not working fine. Only part of my OneOfMyOtherPanels is being shown, not its entirety.

Anyone please guide me on how I should be doing this.

UPDATE: Sorry, I test every panel I build independently by building a main method inside each and putting them on separate frames. This panel I was testing has inside its main method code that adds an additional panel to it as well. Naturally, this other panel was not showing inside my LoginPanel's frame.

Anyways, I'd appreciate any input on whether I am doing this correctly or not. Thank you.

bibiki 18 Posting Whiz

Hey JamesCherrill,
thank you once again for helping me. I tried both ways and I found using ListCellRenderer a little more difficult to use. The problem is that getListCellRendererComponent method returns a component, which forces me to return JLabels, which then don't look nice in my ComboBox.

thank you very much. kind regards.

bibiki 18 Posting Whiz

let me rephrase my question.

I want to pass onto my JComboBox an array of Course objects, but I want it to display my Course objects' coursName field. How do I go about it, anyone?

bibiki 18 Posting Whiz

well, think of a course as not defined by a courseID and courseName only. In my database, two courses may have the same name, but different course books.

or, in a more general sense, think of a table that stores people's names. two people have the same name, but a different ID.

thanks for trying to help though.

bibiki 18 Posting Whiz

Hey there,
I am building a small database app where I allow users to input and retrieve information in and out of database.

As part of my application I have this table courses and a java file Course.java that models a Course object. My Course objects have field variables courseID, courseName and so on.

As part of my app, I have to retrieve all rows from my course table and I put courseName of every object built out of the rows in a ComboBox.

After this, I have to write to another table and put courseID in there, but ComboBox's selected item is a courseName, not a courseID. Now, I know I can do a "SELECT courseID FROM courses WHERE coursName = " + ComboBox.getSelectedItem(), but that seems messy since two courseIDs may be returned by that SELECT statement.

Would anyone tell me please how to do this?

thank you in advance.

bibiki 18 Posting Whiz

I would like to add two things:
one, Edgar Codd was a mathematician. He invented what we call relational databases today. He could not convince even IBM for the benefit of his ideas because even IBM could not see the math in it, I guess.

then, I know many people that hate anything that involves as much math as addition and subtraction if you call it math, but they are way to good in adding and subtracting in practical life.

kind regards.

bibiki 18 Posting Whiz

well see, predicate calculus and set operations are the same thing basically. now, when you need to to perform complex if tests that involve more than evaluation of a single boolean variable, knowledge of predicate calculus/set operations will come in handy allowing you to come up with ways to reduce the amount of code you have to write.

In discreet math they teach you how to simplify a complex predicate expression.

As an example, think of this: you have a form that you use to take input. You want to validate input based on: is it mandatory, or is not? is it numeric only, is it not? is it string, is it not? is it a mix of numbers and alphabetic characters, is it not? you'd have to evaluate a String input against 4 conditions, and you'd want to do this with as little code as possible. first time around, you may do it with lots of code, then when you rewrite your code, you can apply your knowledge of predicate calculus to come up with a shorter way to do it.

BUT, I don't think you really need to complicate things. You can build many applications without knowing much math, as stultuske said. plus, you can hone your skills for organizing code and normalizing databases without knowing any discreet math. but since you asked what math would help you in programming, then Discreet Math and Abstract Algebra, which is pain.. for most people, are two math …

bibiki 18 Posting Whiz

well, let me chip in my two cents.

The math I think you need for the kind of skills I think you want is called Discreet Math. You learn there predicate calculus as thines mentioned. In addition you will learn some graph theory and combinatorics there. This, I believe, would help you in organizing your classes and building your UMLs. This would also give you insight in how to normalize your databases better. Of course, you'd always have to create problems and then solve them if you're to learn.

Not an expert here, just an answer I came up with for my questions.

bibiki 18 Posting Whiz

good then. mark your thread as solved.

bibiki 18 Posting Whiz

JavaPrograms,
I see you got your answer. However, I would like to point out that:
if (die1 || die2 == 1)
gave you problems because die1 is an int and (die2 == 1) is a boolean variable. that is what compiler said. you can't perform or operation on an int and a boolean. what you needed is:
die1 == 1 || die2 == 1
one more thing, in your if-else statements you evaluated the values of die1 and die2. in your switch-case statements, you were evaluating times variable.

bibiki 18 Posting Whiz

check lines 25-27 in your code and see what you might be doing wrong. that is where you're setting that zero that you're getting.

bibiki 18 Posting Whiz

JavaPrograms, as an idea, I would recommend you replace your switch case statement with if-elseif statements and I believe you'll see the problems times variable is giving you. otherwise, NormaR1 has helped you a lot. just take a break, and get back to your code after a few minutes.. and perhaps you'll figure it out

bibiki 18 Posting Whiz

you have for and while loops. you use for loops when you know the exact number of iterations you need. that is, when you have a definite number of iterations to go through. but you use while loops when the number of iterations you will go through is indefinite.

5.things between double quotes are strings. things between single quotes are characters.

5. you use .equals when you compare objects, == otherwise (literal values). Strings are objects, so you use .equals when comparing strings... and so on. read your book friend.

bibiki 18 Posting Whiz

you better study for the final you'll have the next week as you might prepare better for it if you invest tonight's time on it. Java - next time.

bibiki 18 Posting Whiz

alright then. I guess you could mark this thread solved if no help needed anymore.

bibiki 18 Posting Whiz

put the code and error here then, and someone will help you... as far as "why have we to use lot of small panels on the original one ?" goes... It makes mental visualization of the program easier for me, so I thought it would do so for you too.

bibiki 18 Posting Whiz

let us say you have a two dimensional array with dimensions 5, 5. now, you have 25 elements in it, right? every element holds either one or zero. for each element draw a panel. if it is zero, draw road, if it is one draw wall. put each of the 25 panels on a bigger panel. however, you need to specify the x and y position of each small panel within the bigger panel. note that panel on the same row have the same y position, but their x position differs by the width of the smaller panel. for the next row, increase the y position by the height of smaller panels (this is assuming that all small panels have the same dimensions.) hope this helps.

bibiki 18 Posting Whiz

yes ardav,
I use
UPDATE tableName SET columnName = 'someValue'
for questions number 2 and above, but I use an INSERT statement to insert a row with null values for all columns, except the first, auto incremental one.

thank you for your two cents :)

bibiki 18 Posting Whiz

cereal,
I tried that but nothing happened. no error reporting, no row insertion. However, after rewriting my sql statement to include column names as:

INSERT INTO results (column name) VALUES ('val', 'val', 'val'....);

everything went fine. I guess I will mark this thread solved now.

thank you very much.
perhaps I have error reporting turned to off in my server or something.

bibiki 18 Posting Whiz

Hey there,

I am trying to build a small survey for a homework assignment.
So, I have the following code:

$con = mysql_connect("localhost","user", "mypassword");
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}
	
	
	mysql_select_db("mydatabase", $con);

	$slq = "INSERT INTO results VALUES('', '', '', '', '', '', '', '', '', '', '', '')";
	mysql_query($sql, $con);
	
	
	mysql_close($con);

What is even more weird is that I use this same sql statement through command line, as well as through phpMyAdmin, and it inserts a row in my results table in both cases, but it won't insert a row when I use it in a script.

Anyone knows what might I be doing wrong?

thank you.

bibiki 18 Posting Whiz

well,
I am not sure I can help you much with these issues you are having. However, I would like to just give you some feedback which might help you rethink things and perhaps find a solution.

your line fifteen says "do line 17 if there is no more results in rs"

AND, you reset the selected item on line 26 every time there is a result in rs. naturally, this causes the last result on you result set to be selected as the item on cboBreed.

hope this helps a bit.

bibiki 18 Posting Whiz

try this:

"select BreedName from breedmaster where BreedName like(%"+ch+"%)";

this could be one problem. hope this works, if it does not, let us know what exactly is happening so that we can help you more.

bibiki 18 Posting Whiz

you can do the filtering on the database. this is actually very easy in sql based databases.

perhaps a key up handler should be defined. every time a key is released, the database should be queried for records that contain the string you have in your input field, or whatever it is, and then the query result should populate your combobox.

querying database records that contain a string on a field is as easy as:
SELECT * FROM table WHERE field LIKE(%str%);

the % character is important because based on it the database knows if you are looking for records that start with str, or if it does not matter where str is within the desired queries. just do some searching for 'sql like' and you will be fine.

bibiki 18 Posting Whiz

harinath, he is looking for how to store a number with 10^6 digits, not 10^6 itself.

bibiki 18 Posting Whiz

alright mKorbel,
thank you very much. In case I have other questions, I will start a new thread. This got quite long already. See you around.

bibiki 18 Posting Whiz

Hey mKorbel,
thank you for your explanations. I guess there is no table model for your TableWithTimer example. It is using a DefaultTableModel, I believe.

So, if I want to call fireXXXX methods, I should do them inside other methods of TableModel as in, for example:

public void  addRow(){
             //some code that adds row
             fireXXXXX();
}

is that right? So, basically: with a button, I invoke addRow, which then invokes fireXXX, right?

One more thing. One my actual app, I have data on an SQLite database. For populating my table, I usually retrieve all lines. I guess I should specify in my button to only add the last row when a row is inserted in db, also done through the same button...

bibiki 18 Posting Whiz

Well Ezzaral,I don'tunderstand parts 1, 3 and 5.

bibiki 18 Posting Whiz

hey mKorbel,
I was writing my last reply when you wrote your last reply. please accept my apology.

I understand if this is too much and you refuse, but I would greatly appreciate it if you would rewrite my code in accordance with your advice above so that I can see more clearly how to do this.

thank you for all your help.

bibiki 18 Posting Whiz

Thank you Ezzaral.

I replaced my 50th row with data.model = buildData() as well as line 15 to MyTable model, and it now works. However, I am wondering, is this what mKorbel meant or is there perhaps some better, different way to reach this effect?

bibiki 18 Posting Whiz

Hey there,
I know I have marked this thread as solved but I just can't give up on this.

In attempt to more closely follow mKorbel's advice, and after borrowing some code from a short book, I built these two classes: a frame and a button:

import javax.swing.*;
import javax.swing.table.*;

import java.awt.*;

import org.w3c.dom.views.AbstractView;


public class TableFrame extends JFrame{
	public int in = 6;
	Object[][] data = new Object[in][4];//I intend to increment in variable through a button so that I can increase 
	//number of rows in my table and see table refreshing
	JTable table;
	String[] headers = {"One", "Two", "Three", "Four"};//columns
	AbstractTableModel model;
	JScrollPane scrollPane;
	
	public TableFrame()
	{
		setLayout(new GridLayout(2, 1));
		setBounds(50,50,320,400);
		setVisible(true);
		data = buildData();
		
		// make table model with this data..
		model = new MyTable(headers, data);
		table = new JTable(model);
		scrollPane = new JScrollPane(table);
		table.setFillsViewportHeight(true);
		add(scrollPane);
		scrollPane.setBounds(5,5,300,300);
		
		add(new RefreshingButon(this));
	}
	
	public Object[][] buildData()
	{
		Object[][] rez = new Object[in][10];
		
		for (int i=0; i<in; i++)
			for (int j=0; j<10; j++)
			rez[i][j]=i*j;
		
		return rez;
	}
	
	public void increase()
	{
		in++;
		data = buildData();
		model.fireTableDataChanged();
		//validate();
		System.out.println(in);
	}
	
	class MyTable extends AbstractTableModel
	{
		String[] headers;
		Object[][] data;
		
		MyTable(String[] headers, Object[][] data)
		{
			super();
			this.headers=headers;
			this.data=data;
		}
		public int getColumnCount() {
		return headers.length;
		}
		public int getRowCount() {
		return data.length;
		}
		public String getColumnName(int col) {
		return headers[col];
		}
		public Object getValueAt(int row, int col) {
		return data[row][col];
		}
		public Class<?> getColumnClass(int c) {
		return data[0][c].getClass();
		}
		public boolean isCellEditable(int row, int col) {
		return true;
		}
	}
	
	public …
bibiki 18 Posting Whiz

it's easy. All you have to do is check if a diagonal, a row, or a column contains only Xs or Os. If you find such a diagonal, row, or column, the game is over, you have a winner. else, keep going. At the end, you have code to check for a tie.

bibiki 18 Posting Whiz

Ooooo, ok. Thank you Ezzaral.I guess because you both are more experienced you guys understand each other better. I see how I'd do that in my current classes. Thank you!

bibiki 18 Posting Whiz

hehehe
No mKorbel, I did not recreate the whole GUI. I have already designed my little app, and it will be too much to go back and redo my plan. Besides, I need something tangible (as tangible as software gets) to see for my work to keep myself motivated. :)

anyways, thank you again.

bibiki 18 Posting Whiz

Hey friends,
I just wanted to let you know that I solved my problem. I added an invocation of validate() method, and everything works now. I'll mark this thread as solved, as I actually reached the effect I wanted, but I remain puzzled still. Maybe you guys have an answer for me: I tried invoking the frames validate method and it worked just as well as calling container's validate method? What does this mean. Perhaps, frames validate method invokes container's validate method or what?

anyways, I thank you guys for your patience.

bibiki 18 Posting Whiz

hey mKorbel,

thank you for trying to help. Here is what c and cc are:

import javax.swing.*;

import java.awt.*;
import java.sql.Date;
import java.util.ArrayList;

public class MainFrame extends JFrame{
	public MatchesTable table;//custom JPanel, builds a JTable, populates it, and puts it on a panel
	public MatchesForm form;//custom JPanel, puts text fields and labels on a panel to build a form
	public FunctionButtons fub;//custom JPanel that holds a custom JButton that reads MatchesForm text fields, builds a String array, invokes a method for inserting data on database (data taken from form), refreshes the view (at least I want it to refresh it)
	public Container c;//getContentPane()
	public String s = "updated";//I use this string to see if update method's invocation from my button is taking place
	public MatchesDAO dao = new MatchesDAO();//My Data Access Object
	public JPanel cc = new JPanel(new GridLayout(2, 1));//for ease of design, I have two panels. One that holds form, and another that holds my button and my table. my button and table are added on this panel

	public MainFrame()
	{
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		setTitle("Start");
		c = getContentPane();
		form = new MatchesForm();
		c.add(form, BorderLayout.NORTH);
		
		fub = new FunctionButtons(this);
		cc.add(fub);
		table = new MatchesTable();
		cc.add(table);
		c.add(cc, BorderLayout.CENTER);

		pack();
		
		setVisible(true);
	}
	
	public void update()
	{
		//removeAll();
		//c.add(form);
		table = new MatchesTable();
		cc.removeAll();
		//cc.setLayout(new GridLayout(2, 1));
		cc.add(fub);
		cc.add(table);
		cc.repaint();
		c.removeAll();
		c.add(form, BorderLayout.NORTH);
		c.add(cc, BorderLayout.CENTER);
		
		c.repaint();
		repaint();
		System.out.println(s + " " + dao.countRecords());
	}
	
	public static void main(String[] args)
	{
		new MainFrame();
	}
}
bibiki 18 Posting Whiz

Alright, this is how far I got:

After clicking my button, the panel that holds my button and my JTable disappears, only the form is left. When I resize the frame, it the panel reappears, and my table is updated.

This is my update method, which I invoke through the button that also inserts new matches in the database:

public void update()
	{
		
		table = new MatchesTable();//my matches table
		cc.removeAll();//cc is the panel that holds my button fub and my table
		
		cc.add(fub);
		cc.add(table);
		
		c.removeAll();//c is my container
		c.add(form, BorderLayout.NORTH);
		c.add(cc, BorderLayout.CENTER);
		
		c.repaint();
		repaint();
		System.out.println(s + " " + dao.countRecords());
	}

I hope this sounds like something you might have encountered earlier, and that you can direct me further.

bibiki 18 Posting Whiz

Hey James,

What I do is this:

I have a class that constructs my frame and add MatchesTable (the class above) onto another panel, which I add onto my Container in my frame. The way I do this is by reconstructing a

new MatchesTable()

and replacing the current one with it. I do this in my update method inside my frame, which I invoke every time a button is clicked. I expect this to work, it is not working. I tried removeAll() on the panel that actually holds my MatchesTable inside my update method, and it removed the contents of the panel that holds my MatchesTable, but I can't seem to add fresh content on this panel through update() method in the frame.

UPDATE:
Since I am actually declaring a new MatchesTable, the necessary steps for refreshing the contents of JTable are being processed. So, I don't think I need other steps for that. Or am I perhaps wrong?

bibiki 18 Posting Whiz

hey mKorbel,
how are you tonight?

This is my panel that constructs my JTable:

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.table.JTableHeader;
//import java.awt.*;


public class MatchesTable extends JPanel{
	public MatchesDAO dao;//my Data Access Object
	public ArrayList<Match> matches;
	public Object[][] table;//rows
	public Object[] cols = {"Nr", "Nikoqiri", "Mysafiri", "Njish", "Barazim", "Dysh", "Fitues", "Befasi", "Data", "Liga"};
	public JTable tables;//this is my table including the header
	public MatchesTable()
	{
		dao = new MatchesDAO();
		matches = dao.getMatches();
		table = new Object[matches.size()][10];
		int i = 0;
		for(Match mat : matches)
		{
			for(int j = 0; j < 10; j++)
			{
				Object[] k = mat.toObjectArray();
				table[i][j] = k[j];
			}
			i++;
		}
		
		tables = new JTable(table, cols);
		//add(tables);
		JTableHeader header = tables.getTableHeader();
		header.setBackground(Color.GRAY);
		Dimension dim = new Dimension(5, 2);
		  tables.setIntercellSpacing(dim);
		add(new JScrollPane(tables));
	}
	
}

I tried constructing a new MatchesTable inside my frame's overridden update method, but it still did not work. Any ideas how to proceed?

bibiki 18 Posting Whiz

Hey there,
I built the following app;

A frame that with three sections inside it. Each section is actually a panel.

The first panel is a form I use for saving soccer matches on my database;

The second holds only one button that reads textfields from the form above and puts the content on the database table;

The third section/panel holds a JTable that shows the records on my database table matches.

I would like this table to be refreshed upon every time I click the button on the second section, but it is not doing so.

(My button works perfectly fine as far as putting records in the database goes. I also use it to reset to blank text fields in my form.)

I tried frame's update and repaint method, but they ain't worked. I also tried putting all these three panels on a bigger panel, which I then add on the frame, and calling the repaint method on this bigger panel, but that did not work either. Anyone please direct me to get this done.

Thank you.

bibiki 18 Posting Whiz

Alright,
thank you a lot to both of you.

P.S. JamesCherrill, congratulations for being now part of daniweb's hall of fame.