注册jdbc驱动程序的三种方式

来源:互联网 发布:云计算大数据概念股 编辑:程序博客网 时间:2024/05/16 10:45
mysql:

1、比较常用
try{
       Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动
       String url="jdbc:mysql://localhost:3306/databasename";//数据库连接子协议
       Connection conn=DriverManager.getConnection(url,"username","password");
       Statement stmt=conn.createStatement();
       ResultSet rs=stmt.executeQuery("select * from tablename");
       while(rs.next()){//不断指向下一条记录
            System.out.println("DeptNo:"+rs.getInt(1));
            System.out.println("\tDeptName:"+rs.getString(2));
            System.out.println("\tLOC:"+rs.getString(3));
}         
    rs.close();
    stmt.close();
    conn.close();
}catch(ClassNotFoundException e){
   System.out.println("找不到指定的驱动程序类!");
}catch(SQLException e){
    e.printStackTrace();
}


2、通过系统的属性设置
try{
       System.setProperty("jdbc.driver","com.mysql.jdbc.Driver");//系统属性指定数据库驱动
       String url="jdbc:mysql://localhost:3306/databasename";//数据库连接子协议
       Connection conn=DriverManager.getConnection(url,"username","password");
       Statement stmt=conn.createStatement();
       ResultSet rs=stmt.executeQuery("select * from tablename");
       while(rs.next()){//不断指向下一条记录
            System.out.println("DeptNo:"+rs.getInt(1));
            System.out.println("\tDeptName:"+rs.getString(2));
            System.out.println("\tLOC:"+rs.getString(3));
}         
    rs.close();
    stmt.close();
    conn.close();
}catch(SQLException e){
    e.printStackTrace();
}

3、看起来比较直观的一种方式,注册相应的db的jdbc驱动,3在编译时需要导入对应的lib
try{
       new com.mysql.jdbc.Driver();//创建driver对象,加载数据库驱动
       String url="jdbc:mysql://localhost:3306/databasename";//数据库连接子协议
       Connection conn=DriverManager.getConnection(url,"username","password");
       Statement stmt=conn.createStatement();
       ResultSet rs=stmt.executeQuery("select * from tablename");
       while(rs.next()){//不断指向下一条记录
            System.out.println("DeptNo:"+rs.getInt(1));
            System.out.println("\tDeptName:"+rs.getString(2));
            System.out.println("\tLOC:"+rs.getString(3));
}         
    rs.close();
    stmt.close();
    conn.close();
}catch(SQLException e){
    e.printStackTrace();
}


oracel:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class TestOracle {

 public static void main(String[] args) {
  Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
     Class.forName("oracle.jdbc.driver.OracleDriver");//实例化oracle数据库驱动程序(建立中间件)
     String url = "jdbc:oracle:thin:@localhost:1521:oar92";//@localhost为服务器名,sjzwish为数据库实例名
     conn = DriverManager.getConnection(url, "guchao", "jimmy");//连接数据库,a代表帐户,a代表密码
     stmt = conn.createStatement();//提交sql语句,创建一个Statement对象来将SQL语句发送到数据库
    
     //查询数据用executeQuery
     rs = stmt.executeQuery("select * from ruby");//执行查询,(ruby)为表名
     while (rs.next()) {//使当前记录指针定位到记录集的第一条记录
      System.out.println(rs.getString("sid") +" "+ rs.getString("sname"));
     }//1代表当前记录的第一个字段的值,可以写成字段名。
     //2代表当前记录的第二个字段的值,可以写成字段名。
    
     //添加数据用executeUpdate
     //stmt.executeUpdate("insert into ss values(7,'张学友')");
    
     //修改数据用executeUpdate
     //stmt.executeUpdate("update ss set name = '张曼玉' where id = 5");
    
     //删除 数据用executeUpdate
     //stmt.executeUpdate("delete from ss where id = 6");
    
    
    } catch (SQLException e) {
     e.printStackTrace();
    } catch (ClassNotFoundException e) {
     e.printStackTrace();
    }finally{
     try {
      //关闭数据库,结束进程
      rs.close();
      stmt.close();
      conn.close();
     } catch (SQLException e) {
      e.printStackTrace();
     }
    }

 }

}


0 0
原创粉丝点击