话不多说,请看代码:
package com.shsxt.jdbcs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Demo002JDBCConnect {
public static void main(String[] args) throws ClassNotFoundException {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String user= "scott";
String pwd= "tiger";
Connection conn = null;
Statement s = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection(url, user, pwd);
s = conn.createStatement();
String sql = "select deptno, dname, loc from dept";
rs = s.executeQuery(sql);
while(rs.next()){
int deptno = rs.getInt(1); // 根据列号来获取值
String dname = rs.getString("dname"); // 根据列名来获取值
String loc = rs.getString(3);
System.out.println(deptno + "t" + dname + "t" + loc);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(s!=null){
try {
s.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持考高分网!



