JDBC连接MySQL的三种方式
方式一
try { Driver driver = new com.mysql.jdbc.Driver(); String url = "jdbc:mysql://10.180.48.19:3306/hsp_db"; Properties info = new Properties(); info.setProperty("user", "root"); info.setProperty("password", "hillstone"); Connection conn = driver.connect(url, info); System.out.println(conn); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
方式二
try { Driver driver = new com.mysql.jdbc.Driver(); DriverManager.registerDriver(driver); String url = "jdbc:mysql://10.180.48.19:3306/hsp_db"; Properties info = new Properties(); info.setProperty("user", "root"); info.setProperty("password", "hillstone"); Connection conn = DriverManager.getConnection(url, info); System.out.println(conn); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
方式三(推荐)
try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://10.180.48.19:3306/hsp_db"; Properties info = new Properties(); info.setProperty("user", "root"); info.setProperty("password", "hillstone"); Connection conn = DriverManager.getConnection(url, info); System.out.println(conn); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。