1. 赋予scott用户连接权限:Grant connect to scott;
2. 在Myeclipse中新建java项目导入jdbc包(classes12.jar):
右键项目 bulid path -〉add external archives 选择classes12.jar
3. 新建java文件, lianxi01.java
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
importoracle.jdbc.*;
importoracle.jdbc.driver.*;
publicclasslianxi01 {
publicstaticvoid main(String[] args)throws
SQLException, ClassNotFoundException{
//TODO Auto-generated method stub
oracle.jdbc.OracleDriver.isDebug();
Class.forName("oracle.jdbc.driver.OracleDriver");
java.sql.DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
//
String url ="jdbc:oracle:thin:@100.100.100.20:1521:dbsid2";
String user="scott";
String password="tiger";
String sql="select table_name from user_tables";
String sql2="select * from dept";
//
Connection con=java.sql.DriverManager.getConnection(url, user, password);
Statement stmt=con.createStatement();
ResultSet rs=((java.sql.Statement) stmt).executeQuery(sql2);
while(rs.next()){
// String s=rs.getString(2);
System.out.println(rs.getString(1) +"
" + rs.getString(2)+" "+ rs.getString(3));
}
}
}
4. 点击运行,即可进行数据库查询
5. 注: String url一行,@100.100.100.20:1521:dbsid2需要根据实际IP和服务名进行修改。
java程序通过jdbc连接oracle数据库方法,布布扣,bubuko.com