使用JDBC链接oracle数据库的一般流程:
学习是一个不断挑战自我的过程,从不会到会,从不熟悉到熟悉,从菜鸟到高手的过程,其也是一个享受的过程...................................,以下是JDBC链接数据库的6个基本步骤:
import java.sql.*;
public class TestJDBCOrder {
public static void main(String[] args){
[color=green]//step 1: register driver;[/color]
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn=null;
Statement stm=null;
ResultSet rs=null;
try{
[color=green]//step2:connection database[/color]
String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
conn=DriverManager.getConnection(url,"scott","tiger");
[color=green]//step3: create statement[/color]
stm=conn.createStatement();
String sql="select EMPNO,ENAME,SAL " +
"from emp";
[color=green]//step4: execute sql[/color]
rs=stm.executeQuery(sql);
[color=green]//step5: transaction resultset[/color]
while(rs.next()){
System.out.println(rs.getDouble(1)+"----"+rs.getString(2));
}
}catch(Exception e){
e.printStackTrace();
}finally{
[color=green]//step6: release resource[/color]
if(rs!=null) try{rs.close();}catch(Exception e){e.printStackTrace();}
if(stm!=null) try{stm.close();}catch(Exception e){e.printStackTrace();}
if(conn!=null) try{conn.close();}catch(Exception e){e.printStackTrace();}
}
}
}
学习是一个不断挑战自我的过程,从不会到会,从不熟悉到熟悉,从菜鸟到高手的过程,其也是一个享受的过程...................................,以下是JDBC链接数据库的6个基本步骤:
import java.sql.*;
public class TestJDBCOrder {
public static void main(String[] args){
[color=green]//step 1: register driver;[/color]
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn=null;
Statement stm=null;
ResultSet rs=null;
try{
[color=green]//step2:connection database[/color]
String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
conn=DriverManager.getConnection(url,"scott","tiger");
[color=green]//step3: create statement[/color]
stm=conn.createStatement();
String sql="select EMPNO,ENAME,SAL " +
"from emp";
[color=green]//step4: execute sql[/color]
rs=stm.executeQuery(sql);
[color=green]//step5: transaction resultset[/color]
while(rs.next()){
System.out.println(rs.getDouble(1)+"----"+rs.getString(2));
}
}catch(Exception e){
e.printStackTrace();
}finally{
[color=green]//step6: release resource[/color]
if(rs!=null) try{rs.close();}catch(Exception e){e.printStackTrace();}
if(stm!=null) try{stm.close();}catch(Exception e){e.printStackTrace();}
if(conn!=null) try{conn.close();}catch(Exception e){e.printStackTrace();}
}
}
}