首选导入安装路径下jdbc里的jar包(class12.jar),然后创建java工程,编写一个java类,主要代码如下
Connection conn = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
// 注册驱动
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin://10.1.98.13:1521:zzz", "dw_zzz",
"dw_zzz");
} catch (ClassNotFoundException e) {
System.out.println("驱动类没找到");
} catch (SQLException e) {
System.out.println("得到Connection连接对象出错");
}
try {
pstmt = conn.prepareStatement("select * from PRODUCTS");
rs = pstmt.executeQuery();
System.out.println("ID \t PROMC");
while (rs.next()) {
System.out.println(rs.getString("ID") + "\t"
+ rs.getString("PROMC"));
}
} catch (SQLException e0) {
System.out.println("数据库操作异常" + e0.getMessage());
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}