java連線mysql 測試程式碼
阿新 • • 發佈:2018-12-30
package cvit.com.dao; import java.sql.*; public class GetConnection { public Connection getConnection() { Connection conn=null; try { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException ex) { System.out.println("載入驅動程式有錯誤"); } String url = "jdbc:mysql://localhost:3306/eshop?user=root&password=root&useUnicode=true&characterEncoding=gb2312"; conn = DriverManager.getConnection(url); System.out.println("成功連線資料庫!!"); } catch (SQLException ex1) { System.out.print("取得連線的時候有錯誤,請核對使用者名稱和密碼"); } return conn; } public static void main(String[]args) { GetConnection getConn=new GetConnection(); getConn.getConnection(); } }