************************************************************************
****原文:blog.csdn.net/clark_xu 徐长亮的专栏
************************************************************************
1.赋予scott用户连接权限:Grant connect to scott;
2. 在Myeclipse中新建java项目导入jdbc包(classes12.jar):
右键项目 bulid path -〉add external archives 选择classes12.jar
3. 新建java文件, lianxi01.java
importjava.sql.Connection;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importoracle.jdbc.*;
importoracle.jdbc.driver.*;
publicclasslianxi01 {
publicstaticvoidmain(String[] args)throwsSQLException, ClassNotFoundException{
//TODOAuto-generated method stub
oracle.jdbc.OracleDriver.isDebug();
Class.forName("oracle.jdbc.driver.OracleDriver");
java.sql.DriverManager.registerDriver(neworacle.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和服务名进行改动。