没有使用ConnDBa辅助类package two;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SingleOracleJDBC {
public static void main(String[] args) {
Connection conn=null ;
Statement sm=null ;
ResultSet rs=null ;
try {
//1.注册驱动
Class.forName("oracle.jdbc.driver.OracleDriver" );
//2.建立数据库链接 conn = DriverManager.getConnection(url, username, password);
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl" ,"scott" ,"hello" );
//3.创建JDBC Statement对象
sm=conn.createStatement();
//4.读取数据库
rs=sm.executeQuery("select * from dept" );
while (rs.next()){
System.out.println("deptno==" + rs.getInt(1));
System.out.println("dname==" + rs.getString(2));
System.out.println("loc==" + rs.getString(3));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if (rs!=null ){
rs.close();
}
if (sm!=null ){
sm.close();
}
if (conn!=null ){
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Oracle数据库链接(一)
最新推荐文章于 2024-09-03 19:40:04 发布