《加载jdbc数据库驱动三种常见方法》由会员分享,可在线阅读,更多相关《加载jdbc数据库驱动三种常见方法(2页珍藏版)》请在金锄头文库上搜索。
1、第一种Driverimport 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);Conn
2、ection conn = driver.connect(url, info);System.out.println(conn + conn); catch (SQLException e) e.printStackTrace();第二种:DriverManageimport java.sql.*;import java.util.Properties;public class HH public static void main(String args) try Driver driver = new com.mysql.jdbc.Driver();DriverManager.registe
3、rDriver(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 blo
4、cke.printStackTrace();第三种class.forname()import java.sql.*;import java.util.Properties;public class EE public static void main(String args) / TODO Auto-generated method stubtry Class.forName(com.mysql.jdbc.Driver);String url = jdbc:mysql:/localhost:3309/hh;Properties info = new Properties();info.setP
5、roperty(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 blocke.printStackTrace();catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();