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); } }