一、加载oracle驱动jar包,ojdbc6.jar
二、build path *.jar
BuildPath中只支持加入jar文件,具体方法如下: 在eclips里在工程名上右键->build path->contigure bud path->java build path里有个 libraries->
add external jars = 增加工程外部的包;
add jars = 增加工程内包 ;
add library = 增加一个库;
add class folder = 增加一个类文件夹
介绍add library中的User Libraries 添加User Library,具体做法如下:
1. 选中工程右键->build path->Add Libraries...
2. 选择User Library->next
3. 点击User Library按钮
4. 点击new按钮
5. 输入Library name
6.点击ok
7.选中该user library, 然后点击add jars
8.找到对应jar包,依次确定即可。
三、连接数据库
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.ResultSet;
//import java.sql.Statement;
import java.sql.PreparedStatement;
public class Jdbcconnect { public static void main(String[] args) {
final String URL = "jdbc:oracle:thin:@ip/实例名";
final String USER = "数据库用户名";
final String PWD = "密码";
try {
//加载驱动程序 Class.forName("oracle.jdbc.OracleDriver");
System.out.println("加载驱动成功!");
}catch (Exception e){ System.out.println("加载驱动异常"+e);
} try { //获得数据库连接
Connection con = DriverManager.getConnection(URL,USER,PWD);
System.out.println("加载数据库连接成功!");
String sql = "select * from t_sys_user";
PreparedStatement pt = con.prepareStatement(sql);
ResultSet rs = pt.executeQuery();
System.out.println(rs.next());
while(rs.next()) {
String userid = rs.getString("userid");
System.out.println(userid); }
rs.close(); pt.close();
con.close();
}catch (SQLException e){ System.out.println("加载数据库连接异常"+e); } }
}