下面为真正考虑比较全面的JDBC java代码。觉得好的,请帮忙顶下。
import java.sql.*;
public class JDBC {
public static void main(String[] args) {
Connection conn = null;
Statement state = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:192.168.3.111:icss", "scott", "tiger");
state = conn.createStatement();
rs = state.executeQuery("select * from table1");
while (rs.next()) {
System.out.println(rs.getString("deptno"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (state != null) {
state.close();
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public class JDBC {
public static void main(String[] args) {
Connection conn = null;
Statement state = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:192.168.3.111:icss", "scott", "tiger");
state = conn.createStatement();
rs = state.executeQuery("select * from table1");
while (rs.next()) {
System.out.println(rs.getString("deptno"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (state != null) {
state.close();
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}