MySql5.7以上JDBC的CRUD操作

  1 package schiller.com;
  2 
  3 import java.sql.*;
  4 
  5 public class Dbutils {
  6   private String driver = "com.mysql.cj.jdbc.Driver";
  7   private String user = "root";
  8   private String password = "0000";
  9   private String url =
 10       "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai";
 11 
 12   private static Connection conn = null;
 13 
 14   // 获取数据库连接
 15   public Connection getConnection() {
 16     try {
 17       Class.forName(driver);
 18       conn = DriverManager.getConnection(this.url, this.user, this.password);
 19     } catch (ClassNotFoundException e) {
 20       e.printStackTrace();
 21     } catch (SQLException e) {
 22       e.printStackTrace();
 23     }
 24     return conn;
 25   }
 26 
 27   // 查询某条记录
 28   public void selectById(String table, int id) {
 29     try {
 30       PreparedStatement pst =
 31           conn.prepareStatement(String.format("select * from %s where id = %d", table, id));
 32       ResultSet rs = pst.executeQuery();
 33       while (rs.next()) {
 34         String name = rs.getString(1);
 35         int idd = rs.getInt(2);
 36         String sex = rs.getString(3);
 37         System.out.println(idd + " " + name + " " + sex);
 38       }
 39       rs.close();
 40       pst.close();
 41     } catch (SQLException e) {
 42       e.printStackTrace();
 43     }
 44   }
 45 
 46   // 查询全部记录
 47   public void selectAll(String table) {
 48     try {
 49       PreparedStatement pst = conn.prepareStatement(String.format("select * from %s", table));
 50       ResultSet rs = pst.executeQuery();
 51       while (rs.next()) {
 52         String name = rs.getString(1);
 53         int idd = rs.getInt(2);
 54         String sex = rs.getString(3);
 55         System.out.println(idd + " " + name + " " + sex);
 56       }
 57       rs.close();
 58       pst.close();
 59     } catch (SQLException e) {
 60       e.printStackTrace();
 61     }
 62   }
 63 
 64   // 插入一条记录
 65   public void insert(String table, String name, String sex) {
 66     try {
 67       PreparedStatement pst =
 68           conn.prepareStatement(
 69               String.format("insert into %s (name,sex) values ('%s', '%s')", table, name, sex));
 70       int num = pst.executeUpdate();
 71       if (num > 0) {
 72         System.out.println("成功添加一条记录");
 73       } else {
 74         System.out.println("错误");
 75       }
 76       pst.close();
 77     } catch (SQLException e) {
 78       e.printStackTrace();
 79     }
 80   }
 81 
 82   // 删除一条记录
 83   public void deleleById(String table, int id) {
 84     try {
 85       PreparedStatement pst =
 86           conn.prepareStatement(String.format("delete from %s where id = %d", table, id));
 87       int num = pst.executeUpdate();
 88       if (num > 0) {
 89         System.out.println("成功删除一条记录");
 90       } else {
 91         System.out.println("错误");
 92       }
 93       pst.close();
 94     } catch (SQLException e) {
 95       e.printStackTrace();
 96     }
 97   }
 98 
 99   // 更新某条记录
100   public void update(String table, String name, int id, String sex) {
101     try {
102       PreparedStatement pst =
103           conn.prepareStatement(
104               String.format("update %s set name='%s',sex='%s' where id=%d", table, name, sex, id));
105       int num = pst.executeUpdate();
106       if (num > 0) {
107         System.out.println("成功更新一条记录");
108       } else {
109         System.out.println("错误");
110       }
111       pst.close();
112     } catch (SQLException e) {
113       e.printStackTrace();
114     }
115   }
116 }

猜你喜欢

转载自www.cnblogs.com/schiller-hu/p/10958123.html