1、导入所需jar包,在oracle客户端安装目录的\jdbc\lib找到ojdbc14.jar和nls_charset12.jar,将这两个jar包添加到工程中。
(比较简单的做法是:在工程中新建一个目录libs,将jar包拷贝到该目录下,在jar文件上右击,选择Add to Build Path)
2、编写测试代码
import java.sql.*;
public class Main {
String dbUrl = "jdbc:oracle:thin:@oracle数据库IP:端口号:数据库实例名";
String theUser = "mw_sys";
String thePw = "sys";
Connection c = null;
Statement conn;
ResultSet rs = null;
public Main() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
c = DriverManager.getConnection(dbUrl, theUser, thePw);
conn = c.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
rs = conn.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public void close() {
try {
conn.close();
c.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ResultSet rs;
Main conn = new Main();
rs = conn.executeQuery("select * from 表名");
try {
while (rs.next()) {
System.out.println(rs.getString("字段名"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}