(1)先在工程→属性→Java构建路径→库→加载外部JAR,加载classes12.jar
(2)代码
import java.sql.*;
public class java_oracle{
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//得到连接
Connection ct=DriverManager.getConnection("jdbc:oracle:thin:@120.95.134.143:1521:orcl", "lily", "lily");
Statement sm=ct.createStatement();
ResultSet rs=sm.executeQuery("select * from s where sage>20");
while(rs.next())
{
String number=rs.getString(1);
String name=rs.getString(2);
String sex=rs.getString(3);
int age=rs.getInt(4);
String deptment=rs.getString(5);
System.out.printf("%s\t",number);
System.out.printf("%s\t",name);
System.out.printf("%s\t",sex);
System.out.printf("%s\t",age);
System.out.printf("%s\t",deptment);
System.out.println();
}
rs.close();
sm.close();
ct.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
} |
|