第一步:連線MySQL資料庫:public class DBOpenHelper { private static String driver = "com.mysql.jdbc.Driver";// MySQL驅動 private static String url = "jdbc:mysql://IP:xxxx/資料庫";//MYSQL資料庫連線Url private static String user = "root";//使用者名稱 private static String password = "root";//密碼 //連線資料庫 public static Connection getConn(){ Connection conn = null; try { Class.forName(driver);//獲取MYSQL驅動 conn = (Connection) DriverManager.getConnection(url, user, password);//獲取連線 } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } //關閉資料庫 public static void closeAll(Connection conn, PreparedStatement ps){ if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } } //關閉資料庫 public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs){ if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } }}第二步:MySQL資料庫操作: public class DBService { private Connection conn=null; //開啟資料庫物件 private PreparedStatement ps=null;//操作整合sql語句的物件 private ResultSet rs=null;//查詢結果的集合 //DBService 物件 public static DBService dbService=null; // 構造方法,私有化 private DBService(){ } //獲取MySQL資料庫單例類物件 public static DBService getDbService(){ if(dbService==null){ dbService=new DBService(); } return dbService; } // 獲取資料 public List
第一步:連線MySQL資料庫:public class DBOpenHelper { private static String driver = "com.mysql.jdbc.Driver";// MySQL驅動 private static String url = "jdbc:mysql://IP:xxxx/資料庫";//MYSQL資料庫連線Url private static String user = "root";//使用者名稱 private static String password = "root";//密碼 //連線資料庫 public static Connection getConn(){ Connection conn = null; try { Class.forName(driver);//獲取MYSQL驅動 conn = (Connection) DriverManager.getConnection(url, user, password);//獲取連線 } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } //關閉資料庫 public static void closeAll(Connection conn, PreparedStatement ps){ if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } } //關閉資料庫 public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs){ if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } }}第二步:MySQL資料庫操作: public class DBService { private Connection conn=null; //開啟資料庫物件 private PreparedStatement ps=null;//操作整合sql語句的物件 private ResultSet rs=null;//查詢結果的集合 //DBService 物件 public static DBService dbService=null; // 構造方法,私有化 private DBService(){ } //獲取MySQL資料庫單例類物件 public static DBService getDbService(){ if(dbService==null){ dbService=new DBService(); } return dbService; } // 獲取資料 public List