packagecom;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
/**
*定义数据类连接数据库且查询信息
*@authorBR
*@date2015-08-13
*@version1.o
*/
publicclassdataBase{
publicstaticvoidmain(String[]args){
Connectionconnection=null;
try{
//加载驱动
Class.forName(“oracle.jdbc.driver.OracleDriver”);
//获取数据库连接
connection=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:orcl”,”scott”,”scott”);
//查询语句
Stringsql=”select*fromdept”;
//创建Statement对象
Statementstatement=connection.createStatement();
ResultSetrSet=statement.executeQuery(sql);
//判断查询的结果集是否为空
if(rSet!=null){
while(rSet.next()){
intnum=rSet.getInt(“deptNo”);
Stringname=rSet.getString(“dName”);
Stringloc=rSet.getString(3);
System.out.println(num+”\t”+name+”\t”+loc);
}
}
//关闭资源
rSet.close();
statement.close();
connection.close();
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}