multiple resultset

Hi,

I'm trying to execute a specific query by clicking a button.I was trying to use inside actionperformed code different Statement and resultSets but every time throws errors.I'll be appreciated if someone give me an example similar to the previous description.

Thanks

[290 byte] By [mpelasa] at [2007-11-27 11:41:06]
# 1

Post some code.

What are database operations doing inside an ActionListener? Better to move them to a separate class (preferrably interface based) that you can test without the UI. Then have your ActionListener simply call those methods.

%

duffymoa at 2007-7-29 17:36:13 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

//Connection to Database

private void dbConn(){

try {

String mydriver="com.mysql.jdbc.Driver";

String myURL="jdbc:mysql://localhost:3306/packets_db";

String userName = "root";

String password = "";

Class.forName(mydriver);

Connection dbConnection=DriverManager.getConnection(myURL,userName,password);

String MySelectString=" SELECT * FROM `packet` ";

try {

MySelectString=MySelectString.trim();

Statement mystate=dbConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

rs=mystate.executeQuery(MySelectString);

}

catch (SQLException sqle) {

System.out.println("ERROR"+sqle);

}

}

catch (SQLException sqle){

System.out.println("Den yparxei i basi dedomenon!!!!");

} catch (ClassNotFoundException cnfe) {

System.out.println("Den yparxei odriver!!!!!");

}

}

//i'm tying to execute sqlquery

boolean found=false;

try {

dbConn();

while(rs.next()){

Date str=rs.getDate("str_date");

Date end=rs.getDate("end_date");

int IDint=rs.getInt("packet_id");

Date sd=jDateChooser1.getDate();

Date ed=jDateChooser2.getDate();

String sqlquery="SELECT packet_id FROM `packet` WHERE str_date>=sd && end_date<=ed";

rs=mystate.executeQuery(sqlquery);

System.out.println(IDint);

found=true;

}

} catch (SQLException eee) {

System.out.println("ERROR"+eee);

}

if(!found){

jLabel4.setText("***No packet found!!!***");

}

mpelasa at 2007-7-29 17:36:13 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

//I'm trying to execute the following Query but throws Statements and //ResultSet errors

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

boolean found=false;

try {

dbConn();

while(rs.next()){

Date str=rs.getDate("str_date");

Date end=rs.getDate("end_date");

int IDint=rs.getInt("packet_id");

Date sd=jDateChooser1.getDate();

Date ed=jDateChooser2.getDate();

String sqlquery="SELECT packet_id FROM `packet` WHERE str_date>=sd && end_date<=ed";

rs=mystate.executeQuery(sqlquery);

System.out.println(IDint);

found=true;

}

} catch (SQLException eee) {

System.out.println("ERROR"+eee);

}

if(!found){

jLabel4.setText("***No packet found!!!***");

}

}

any Help?

Thanks

mpelasa at 2007-7-29 17:36:13 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

> //I'm trying to execute the following Query but

> throws Statements and //ResultSet errors

can you post the errors.

Yannixa at 2007-7-29 17:36:13 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

for my ResultSet "rs"

any Help?

mpelasa at 2007-7-29 17:36:13 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

try {

MySelectString=MySelectString.trim();

Statement mystate=dbConnection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

rs=mystate.executeQuery(MySelectString);

}

catch (SQLException sqle) {

System.out.println("ERROR"+sqle);

}

I do not see where you declared rs as a ResultSet. Try putting in "ResultSet rs = mystate.executeQuery(MySelectString);"

Scuba_Stevea at 2007-7-29 17:36:13 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...