zoukankan      html  css  js  c++  java
  • 注册jdbc驱动程序的三种方式

    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();
    }
  • 相关阅读:
    mysql 7.5.8 服务无法启动 服务没有报告任何错误
    Ubuntu 16.04 php卸载
    函数式编程(3)-匿名函数
    函数式编程(2)-返回函数
    函数式编程(1)-高阶变成(3)-sorted
    函数式编程(1)-高阶变成(2)-filter
    函数式编程(1)-高阶变成(1)-map/reduce
    高级特性(4)-生成器
    高级特性(3)-列表生成式
    高级特性(2)-迭代
  • 原文地址:https://www.cnblogs.com/elleniou/p/3024994.html
Copyright © 2011-2022 走看看