package exam;
import java.sql.DriverManager;//驱动器的管理用以选择驱动器创建连接
import java.sql.Connection;//代表与某一数据库的链接,支持sql声明的创建
import java.sql.Statement;//在链接中执行静态的sql声明,并取得执行结果
import java.sql.ResultSet;//代表执行sql声明后产生的数据结果
import java.sql.SQLException;
public class TestDB {
public static void main(String[] args) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");//java驱动程序
String url = "jdbc:oracle:thin:@127.0.0.1:1521:WORKDB";//url串
Connection conn = DriverManager.getConnection(url,"dbtest","123456");//连接数据库的url串,用户密码,用户名;
Statement stmt = conn.createStatement();//创建数//据库通道
// ResultSet rs = stmt.executeQuery("select * from song");//创建数据库结果集
// ResultSet rs = stmt.executeQuery("insert into BJXXB(bh,bjmc,bzxh) values('6','song','qiang')");
// ResultSet rs = stmt.executeQuery("select * from BjXXB");
//ResultSet rs= stmt.executeQuery("delete from BJXXB where BH=2");
//ResultSet rs = stmt.executeQuery("select * from BjXXB");
// ResultSet rs = stmt.executeQuery("update bjxxb set bjmc='luo' where bh=1");
ResultSet rs = stmt.executeQuery("SELECT * FROM (SELECT A.*, ROWNUM RN FROM (SELECT * FROM BJXXB) A WHERE ROWNUM <= 5)WHERE RN >= 1");
while(rs.next()){
System.out.println("bjmc:"+rs.getString(3));
System.out.println("bh:"+rs.getString(1));
// System.out.println("name:"+rs.getString(1));
//System.out.println("name:"+rs.getString(2));
//System.out.println("id:"+res.getInt(1));
//System.out.println("id:"+rs.getInt(2));
}
rs.close();//关闭结果集
stmt.close();//关闭结果集通道
conn.close();//关闭数据库连接
}catch(ClassNotFoundException e){
e.printStackTrace();
System.out.println("找不到服务!");
}catch(SQLException e){
e.printStackTrace();
}
}
}
阅读(932) | 评论(0) | 转发(0) |