1、用eclipse鏈接mysql數據庫:html
要先添加一個文件:http://www.javashuo.com/article/p-yyynfalq-gh.htmljava
2、開始寫鏈接數據庫的代碼:mysql
由於鏈接數據庫的操做通常是公用的,因此寫在叫作util的包裏sql
像實體類通常寫在叫作entity包裏面數據庫
測試類(JUnit裏講解過怎麼使用)寫在叫作test的包裏面eclipse
package com.util; import java.sql.*; public class ConnectionUtil { /** * 第一步:加載驅動 * 第二步:連接數據庫 * 第三步:必定要關閉流 * 第四步:測試是否鏈接成功 */ private static String DRIVER = "com.mysql.cj.jdbc.Driver"; // 數據庫驅動 private static String URL = "jdbc:mysql://localhost:3306/xxxy?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT"; // 訪問數據庫路徑 //xxxy 是我鏈接的數據庫名稱 private static String NAME = "root"; // 數據庫用戶名 private static String PASSWORD = "root"; // 數據庫密碼 public static Connection getConnection() { Connection connection = null; try { // 加載驅動 Class.forName(DRIVER); // 鏈接數據庫 connection = DriverManager.getConnection(URL, NAME, PASSWORD); return connection; } catch (Exception e) { return null; } } // 關閉流 public static void closeConnection(Connection connection) { try { connection.close(); } catch (Exception e) { e.printStackTrace(); } } public static void closeStatement(Statement statement) { try { statement.close(); } catch (Exception e) { e.printStackTrace(); } } public static void closePreparedStatement(PreparedStatement pStatement) { try { pStatement.close(); } catch (Exception e) { e.printStackTrace(); } } public static void closeResultSet(ResultSet rs) { try { rs.close(); } catch (Exception e) { e.printStackTrace(); } } // 測試數據庫是否連接成功 /*public static void main(String[] args) { System.out.println("good"+getConnection()); }*/ }
3、寫成DAO模式的增刪改查操做(寫成這樣代碼更加規範!)測試
DAO模式能夠把實現數據庫表的操做轉化爲對JAVA類的操做spa
/* read all data */ public static List<Users> readDao() { String sql = "select * from Journalism"; Connection connect = null; PreparedStatement ptmt = null; ResultSet rs = null; List<Users> lists = new ArrayList<Users>(); try { connect = ConnectionUtil.getConnection(); ptmt = connect.prepareStatement(sql); rs = ptmt.executeQuery(); while (rs.next()) { Users user = new Users(); user.setType(rs.getString("type")); user.setContent(rs.getString("content")); user.setTitle(rs.getString("title")); user.setAuthor(rs.getString("author")); user.setId(rs.getString("id")); user.setImg(rs.getString("img")); user.setToday(rs.getTimestamp("today")); lists.add(user); } return lists; } catch (Exception e) { e.printStackTrace(); return null; } finally { ConnectionUtil.closeResultSet(rs); ConnectionUtil.closePreparedStatement(ptmt); ConnectionUtil.closeConnection(connect); } } /* delete the data */ public static boolean deleteDao(Users user) { String sql = "delete from Journalism where id = ?"; Connection connect = null; PreparedStatement ptmt = null; try { connect = ConnectionUtil.getConnection(); ptmt = connect.prepareStatement(sql); ptmt.setString(1, user.getId()); int i = ptmt.executeUpdate(); return i > 0 ? true : false; } catch (Exception e) { e.printStackTrace(); return false; } finally { ConnectionUtil.closePreparedStatement(ptmt); ConnectionUtil.closeConnection(connect); } }