how to search in a table after two fields

Hi! I have another problem, in that same project!you know i have the rent table from the database! This communicates with java in a jFrame!in this jFrame i have a jTable with te columns and values from the rent table from the dbase,i have a search button,and 4 JtextFields for clientNAme,bookName,date,price!

So I have to do a search over any 2 jTextfields in the rent table! For exempoe Search for numeCli =jTextFieldCli.getText() and priceextFieldPriceext().

I don't know how to write the sintax. i should do something like this

String search = "Select from rent where numCli =" + nume+ and .....

Can somebody please help me!

Thanks,and sorry for the long message!

[706 byte] By [Killer86a] at [2007-11-27 2:10:38]
# 1

String field1 = yourTextField1.getText();

String field2 = yourTextField2.getText();

String select = "SELECT * FROM TABLE WHARE Column1Name='"+field1+"' AND column2Name = '"+field2+"'";

//create connection

//create statement (use select string for this)

//get result set (data)

hope that helps

java_2006a at 2007-7-12 2:03:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

You should use a PreparedStatement for this or you might run into further problems:

sql =" SELECT * FROM TABLE WHERE id=? and name=?";

PreparedStatement pstmt = conn.prepareStatement(sql);

pstmt.setInt(1,666);

pstmt.setString(2,"Mr Devil");

ResultSet rs = pstmt.execute();

Of course you should also make good use of try catch and finally and not hack at it like my example.

ChristopherAngela at 2007-7-12 2:03:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
Thanks for all! Java_2006 i know that what you have written is good,and working but i dont know the user which textfield will choose,so i should use in the sintax all the 4 j textfields at the same time!How can i do that?
Killer86a at 2007-7-12 2:03:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

I don't know what do you need exactly, so I suggest this to help u :

String field1 = yourTextField1.getText();

String field2 = yourTextField2.getText();

String field3 = yourTextField3.getText();

String field4 = yourTextField4.getText();

String select = "";

if(field1!=null&&field1.trim().length()>0 && field2!=null&&field2.trim().length()>0){//1 and 2 not empty

select = "SELECT * FROM TABLE WHERE Column1Name='"+field1+"' ANDcolumn2Name = '"+field2+"'";

}else if(field3!=null&&field3.trim().length()>0 && field3!=null&&field3.trim().length()>0){//3and 4 not empty

select = "SELECT * FROM TABLE WHERE Column3Name='"+field3+"' ANDcolumn4Name = '"+field4+"'";

}else if(...){//1 and 3 not empty

select = ...

}

.... //other else if

else{//all the text fields are empty

//error.

}

BTW, if you'll call your select in a loop, so use PreparedStatement as previously said.

hope that helps

java_2006a at 2007-7-12 2:03:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

String field1 = yourTextField1.getText();

String field2 = yourTextField2.getText();

String field3 = yourTextField3.getText();

String field4 = yourTextField4.getText();

String select = "SELECT * FROM TABLE WHERE Column1Name='"+field1+"' ANDcolumn2Name = '"+field2+"' ANDcolumn3Name = '"+field3+"' ANDcolumn4Name = '"+field4+"'";

java_2006a at 2007-7-12 2:03:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

Java_2006 he sintax is just good!But i have a question!If one of my fields is not string, it's int : then how can i verify if it's not empty?

Because i can't write field1!=null&&field1.trim because i can't use it for int!Can you help me?

if you can do this 4 me than my project is ready!

thanks again java_2006

Killer86a at 2007-7-12 2:03:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
An int is a primitive datatype and definitely not an Object, so it cannot be null. The default value of an int is just 0 (zero).
BalusCa at 2007-7-12 2:03:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8

Use is null

Example :

String select = "SELECT person_name FROM person_table p WHERE p.person_id is null";

BTW, don't use simple quote for numeric data.

Example :

String select = "SELECT person_name FROM person_table p WHERE p.person_id = "+id;

java_2006a at 2007-7-12 2:03:00 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...