• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC question!

 
ping shx
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my java programme:
import java.sql.*;
public class bb
{
public static void main(String args[ ])
{
Connection connection=null;
Statement statement=null;
ResultSet resultset;
Class.forName("oracl.jdbc.OracleDriver").newInstance() ;
connection=DriverManager.getConnection("jdbc racl:thin:@10.10.10.15:1521 8inext","system","manager") ;
statement=connection.createStatement() ;
resultset=statement.executeQuery("select user,sysdate from dual") ;
System.out.println(resultset.getString(1));
}
}
when I run :
C:\>javac c:\bb.java
c:\bb.java:10: unreported exception java.lang.ClassNotFoundException; must be ca
ught or declared to be thrown
Class.forName("oracl.jdbc.OracleDriver").newInstance() ;
^
c:\bb.java:10: unreported exception java.lang.InstantiationException; must be ca
ught or declared to be thrown
Class.forName("oracl.jdbc.OracleDriver").newInstance() ;
^
c:\bb.java:11: unreported exception java.sql.SQLException; must be caught or dec
lared to be thrown
connection=DriverManager.getConnection("jdbc racl:thin:@10.10.10.15:1521 8
inext","system","manager") ;
^
c:\bb.java:12: unreported exception java.sql.SQLException; must be caught or dec
lared to be thrown
statement=connection.createStatement() ;
^
c:\bb.java:13: unreported exception java.sql.SQLException; must be caught or dec
lared to be thrown
resultset=statement.executeQuery("select user,sysdate from dual") ;
^
c:\bb.java:14: unreported exception java.sql.SQLException; must be caught or dec
lared to be thrown
System.out.println(resultset.getString(1));
^
6 errors
WHY?? Thanks!!
 
Andy Bowes
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Looks link this Post should be in the JDBC forum.
Anyway, your code isn't to bad but you are just missing any Exception handling. Java insist that you deal with exceptions either by catching and handling them or by passing them back to the invoking method.
To handle the exception you will need to add a try{} catch (){} block to your code. NB Also added finally block to ensure that resources are closed.
public static void main(String args[ ])
{
Connection connection=null;
Statement statement=null;
ResultSet resultset;
try{
Class.forName("oracl.jdbc.OracleDriver").newInstance() ;
connection=DriverManager.getConnection("jdbc racl:thin:@10.10.10.15:1521 8inext","system","manager") ;
statement=connection.createStatement() ;
resultset=statement.executeQuery("select user,sysdate from dual") ;
System.out.println(resultset.getString(1));
}
catch (java.sql.SQLException x )
{
x.printStaceTrace();
}
finally{
if ( resultset != null){ try{resultset.close();} catch (Exception e){}}
if ( statement != null){try{statement.close();} catch (Exception e){}}
if ( connection != null){try{connection.close();} catch (Exception e){}}
}
}
[ June 27, 2003: Message edited by: Andy Bowes ]
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to JDBC as sugegsted.
 
SJ Adnams
Ranch Hand
Posts: 925
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just as an aside, the closing of the resultset is redundant since if you close the statement the resultset will also be closed.
however I think this point relates more to coding style.
personally I would do an rs.close() inside the try block, but only a stmt.close() in the finally block.
[ June 27, 2003: Message edited by: Simon Lee ]
 
ping shx
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, Thank everyone! now I haved changed my code,there is new question:
import java.sql.*;
import java.lang.*;
//import oracle.jdbc.driver.*;
public class dd
{
public static void main(String args[ ])
{
Connection connection=null;
Statement statement=null;
ResultSet resultset;
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance() ;
connection=DriverManager.getConnection("jdbc racl:thin:@10.10.10.15:1521 8inext","system","manager") ;
statement=connection.createStatement() ;
resultset=statement.executeQuery("select user,sysdate from dual") ;
System.out.println(resultset.getString(1));
resultset.close();
statement.close();
connection.close();
}
//catch (java.sql.SQLException x )
catch (Exception x)
{
//x.printStaceTrace();
System.out.println(x);
}
/*finally{
if ( resultset != null){ try{resultset.close();} catch (Exception e){}}
if ( statement != null){try{statement.close();} catch (Exception e){}}
if ( connection != null){try{connection.close();} catch (Exception e){}}
}*/
}

}
C:\>javac c:\dd.java
C:\>java -cp . dd
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
I'm java a newer,my envirment:window2000+oracle8.1.5+jbuiler9
Thanks!
[ June 28, 2003: Message edited by: ping shx ]
[ June 28, 2003: Message edited by: ping shx ]
 
ping shx
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ping shx:
[QB]hi, Thank everyone! now I haved changed my code,there is new question:
import java.sql.*;
import java.lang.*;
//import oracle.jdbc.driver.*;
public class dd
{
public static void main(String args[ ])
{
Connection connection=null;
Statement statement=null;
ResultSet resultset;
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance() ;
connection=DriverManager.getConnection("jdbc oracle:thin:@10.10.10.15:1521 8inext","system","manager") ;
statement=connection.createStatement() ;
resultset=statement.executeQuery("select user,sysdate from dual") ;
System.out.println(resultset.getString(1));
resultset.close();
statement.close();
connection.close();
}
//catch (java.sql.SQLException x )
catch (Exception x)
{
//x.printStaceTrace();
System.out.println(x);
}
/*finally{
if ( resultset != null){ try{resultset.close();} catch (Exception e){}}
if ( statement != null){try{statement.close();} catch (Exception e){}}
if ( connection != null){try{connection.close();} catch (Exception e){}}
}*/
}

}
C:\>javac c:\dd.java
C:\>java -cp . dd
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
I'm java a newer,my envirment:window2000+oracle8.1.5+jbuiler9
Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic