jdbc连接oracle数据库功能示例
本文实例讲述了jdbc链接oracle数据库功能。分享给大家供大家参考,具体如下:
packagecom.sefvang.jdbc;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
publicclassDb_conn{
privatestaticfinalStringurl="jdbc:oracle:thin:@localhost:1521:orcl";
privatestaticfinalStringname="system";
privatestaticfinalStringpasswd="sefvang";
privatestaticfinalStringDb_driver="oracle.jdbc.OracleDriver";
publicConnectiongetConnection(){
Connectionconn=null;
try{
Class.forName(Db_driver);
conn=DriverManager.getConnection(url,name,passwd);
if(conn!=null){
System.out.println("success!!!");
}
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnconn;
}
publicvoidclose(Connectionconn){
try{
if(conn!=null){
if(!conn.isClosed()){
conn.close();
}
}
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
Db_connconn=newDb_conn();
Connectioncon=conn.getConnection();
try{
PreparedStatementpapt=con.prepareStatement("selecttnamefromtextwheretid=1");
ResultSetrs=papt.executeQuery();
if(rs.next())
System.out.println(rs.getString(1));
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
conn.close(con);
}
}
希望本文所述对大家java程序设计有所帮助。