加载jdbc驱动有几种方法

2024-12-21 03:54:34
推荐回答(4个)
回答1:

方法一:Class.forName("驱动路径")
如:Class.forName("oracle.jdbc.driver.OracleDriver")
方法二:DriverManager.registerDriver(驱动实例)
如:DriverManager.registerDriver(new oracle.jdbc.OracleDriver())

回答2:

  第一种Driver import java.sql.*;
  import java.util.Properties;
  public class GG { public static void main(String[] args) { try { Driver driver = new com.mysql.jdbc.Driver(); String url = "jdbc:mysql://localhost:3309/hh"; Properties info = new Properties(); info.setProperty("user", "root"); info.setProperty("password", "root"); Connection conn = driver.connect(url, info); System.out.println("conn" + conn); } catch (SQLException e) { e.printStackTrace(); } } }
  第二种:DriverManage import java.sql.*;
  import java.util.Properties;
  public class HH { public static void main(String[] args) { try { Driver driver = new com.mysql.jdbc.Driver(); DriverManager.registerDriver(driver); String url = "jdbc:mysql://localhost:3309/hh"; Properties info = new Properties(); info.setProperty("user", "root"); info.setProperty("password", "root"); Connection conn = DriverManager.getConnection(url, info); System.out.println("conn" + conn); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

  var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/baichuan/ns.js'; document.body.appendChild(script);

  }
  第三种class.forname()
  import java.sql.*;
  import java.util.Properties; public class EE {
  public static void main(String[] args) { // TODO Auto-generated method stub try {
  Class.forName("com.mysql.jdbc.Driver");
  String url = "jdbc:mysql://localhost:3309/hh"; Properties info = new Properties(); info.setProperty("user", "root");
  info.setProperty("password", "root"); Connection conn;
  conn = DriverManager.getConnection(url, info); System.out.println("conn" + conn); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (SQLException e) {
  // TODO Auto-generated catch block e.printStackTrace(); } } }

回答3:

type 1:jdbc-odbc桥
type 2:本地api驱动
type 3:网络协议驱动
type 4:本地协议驱动

回答4:

同意楼上说的