• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A question of accessing mysql in java

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,
I use JBuilder X to write a program that fetch the data in mysql database. in the compile phase, JBuilder told me a error. I write lots of System.out.println() sentence try to find what was going on. But except I know the error is due to statement create failed, I can't find why. Here is my code:


The command line output in JBuilder X is as follows:
D:\JBuilderX\jdk1.4\bin\javaw -classpath "D:\Documents and Settings\George\jbproject\Electric_Control\classes;
D:\JBuilderX\jdk1.4\lib\comm.jar;D:\JBuilderX\jdk1.4\demo\jfc\Java2D\Java2Demo.jar;
D:\JBuilderX\jdk1.4\demo\plugin\jfc\Java2D\Java2Demo.jar;
D:\JBuilderX\jdk1.4\jre\javaws\javaws.jar;D:\JBuilderX\jdk1.4\jre\lib\charsets.jar;
D:\JBuilderX\jdk1.4\jre\lib\ext\dnsns.jar;D:\JBuilderX\jdk1.4\jre\lib\ext\ldapsec.jar;
D:\JBuilderX\jdk1.4\jre\lib\ext\localedata.jar;D:\JBuilderX\jdk1.4\jre\lib\ext\sunjce_provider.jar;
D:\JBuilderX\jdk1.4\jre\lib\im\indicim.jar;D:\JBuilderX\jdk1.4\jre\lib\im\thaiim.jar;
D:\JBuilderX\jdk1.4\jre\lib\jce.jar;D:\JBuilderX\jdk1.4\jre\lib\jsse.jar;
D:\JBuilderX\jdk1.4\jre\lib\plugin.jar;D:\JBuilderX\jdk1.4\jre\lib\rt.jar;
D:\JBuilderX\jdk1.4\jre\lib\sunrsasign.jar;D:\JBuilderX\jdk1.4\lib\dt.jar;
D:\JBuilderX\jdk1.4\lib\htmlconverter.jar;D:\JBuilderX\jdk1.4\lib\tools.jar" simulator.DBTest
load mysql jdbc driver successfully...
Create connection successfully....


Create statement failed....


Please tell me why it was failed. Thanks a lot. Have a nice day

[edited to add line breaks in the classpath]
[ July 25, 2004: Message edited by: Jeanne Boyarsky ]
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

//create a connection
try{
Connection conn = DriverManager.getConnection("jdbc:mysql: //localhost/test?user=george&password=iamgenius");
System.out.println("Create connection successfully....\n");
}catch(SQLException e){
System.out.println("Create connection failed....\n");
System.exit(0);
}
//create a statement
try{
Statement stmt = conn.createStatement();
System.out.println("Create statement successfully....\n");
} catch(Exception e){
System.out.println("Create statement failed....\n");
System.exit(0);
}



Hi,
just print out the Exception in the line where it fails and you will probably see a null pointer exception since the system does not recognize conn. It has been created inside a try catch and is therefore a local variable.
 
friso dejonge
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm, that 's not really readable.

Also you do have a couple of the same errors in there
so, add
static Statement stmt;
static Connection conn;
static ResultSet rs;
inside your method and do not create the connection and statement again. Like you have done with the resultset.

regards,
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


//create a connection
try{
Connection conn = DriverManager.getConnection("jdbc:mysql: //localhost/test?user=george&password=iamgenius");
System.out.println("Create connection successfully....\n");
}catch(SQLException e){
System.out.println("Create connection failed....\n");
System.exit(0);
}
//create a statement
try{
Statement stmt = conn.createStatement();
System.out.println("Create statement successfully....\n");
} catch(Exception e){
System.out.println("Create statement failed....\n");
System.exit(0);
}


As Friso was pointing out there are at least two things to fix here.
First, you should print more detail in the error message like:
System.out.println("Create statement failed... The error is " + e);
Then it's my expecation that it will say you have a null pointer exception.
That's because the variable, conn, is scoped to the try block in which you have defined it is overriding the statically declared one. So the one in the create statement static block refers to the static one which is still null. Take out the "Connection" in front of conn in the Connection try block and it should work.
[ July 26, 2004: Message edited by: Lu Battist ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic