步骤:
1、先找到classes12.jar文件,该文件在你安装oracle目录下,可以去找,要是不知道放哪,可以通过搜索该文件找到文件所在的目录
2、在一个Eclipse里创建一个java项目,在该项目属性界面,点击Java Build Path ->Libraries->点击Add External JARs 添加你刚刚搜索到的classes12.jar文件
3、编写代码如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class db {
private String url = "jdbc:oracle:thin:@10.10.21.48:1521:ORCL";
private String usename = "SCOTT";
private String pw = "password";
private Connection conn = null;
public Connection Conn1()
{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
try {
conn = DriverManager.getConnection(url,usename,pw);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
db obj = new db();
Connection con = obj.Conn1();
if(con == null)
System.out.print("连接失败");
else
System.out.print("连接成功");
}
}