A, is connected with the eclipse mysql database:
First to add a file: https://www.cnblogs.com/caiwenjing/p/8079227.html
Second, start writing code to connect to the database:
Because the operation of the database connection is generally common, so write in the bag, I called the util
Like entity called the entity class generally written in the package inside
Test classes (JUnit in his explanations how to use) is called in to write a test packet inside
Package com.util; Import the java.sql *. ; public class ConnectionUtil { / ** * Step: Load Driver * Step: Database Link * Step: Be sure to close the flow * Step Four: Test whether the connection success * / Private static String dRIVER = "com.mysql.cj.jdbc.Driver"; // database-driven Private static String the URL of = "jdbc: MySQL: // localhost: 3306 / = to true XXXY useUnicode & characterEncoding = UTF-8 & useSSL =? & serverTimezone GMT = false "; // access the database path // XXXY is the name of my database connection Private staticNAME = String "the root"; // database username Private static String = PASSWORD "the root"; // database password public static Connection the getConnection () { Connection Connection = null ; the try { // load the driver Class.forName (DRIVER); // connect to the database connection = the DriverManager.getConnection (the URL, NAME, PASSWORD); return connection; } the 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()); }*/ }
Third, deletions written DAO mode change search operation (so that the code written specification more!)
DAO mode can be implemented into the operation database table operation of the JAVA class
/* 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); } }