增加

  1. package test;
  2. import java.io.BufferedInputStream;
  3. import java.io.FileInputStream;
  4. import java.sql.*;
  5. import java.util.Properties;
  6. public class ConnectionTest {
  7. public static void main(String[] args) throws Exception {
  8. BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("E:\\IdeaProjects\\Demo\\src\\test\\database.properties"));
  9. Properties properties = new Properties();
  10. properties.load(inputStream);
  11. String url = properties.getProperty("url");
  12. String user = properties.getProperty("user");
  13. String password = properties.getProperty("password");
  14. String driverStr = properties.getProperty("driver");
  15. Class.forName(driverStr);
  16. Connection driverManager = DriverManager.getConnection(url, user, password);
  17. //==========================================================================
  18. // ?表示占位符
  19. String sql = "insert into student(sid,sname,sage,classid) values(?,?,?,?)";
  20. PreparedStatement ps = driverManager.prepareStatement(sql);
  21. ps.setInt(1,5);
  22. ps.setString(2,"ps");
  23. ps.setInt(3,19);
  24. ps.setInt(4,4);
  25. ps.execute(); //执行sql语句
  26. }
  27. }

增 删 改 只是sql语句和占位符不一样 可以写成通用方法

  1. // 传入sql语句 和 占位符的真实值
  2. public void update(String sql, Object... args) {
  3. Connection connection = null;
  4. PreparedStatement preparedStatement = null;
  5. try {
  6. connection = ConnSql.conncationSql();
  7. preparedStatement = connection.prepareStatement(sql);
  8. for (int i = 0; i < args.length; i++) {
  9. // 循环填充占位符
  10. preparedStatement.setObject(i + 1, args[i]);
  11. }
  12. // 执行sql语句
  13. preparedStatement.execute();
  14. } catch (Exception e) {
  15. e.printStackTrace();
  16. }finally {
  17. // 关闭
  18. ConnSql.close(connection,preparedStatement);
  19. }
  20. }

查询

  1. getColumnName 获取列名
  2. getColumnLabel 获取列的别名
  3. 注意: 使用getColumnLabel时,列没有别名 会使用列名,可以解决数据库中表的列名,跟java中类属性名不一致的问题
  1. // 获取元数据 ResultSetMetData类中的方法可以查看表中列的信息
  2. ResultSetMetData resultMetData = res.getMetData
  3. //==========================================================================
  4. // 利用反射的技术 给对象赋值
  5. // 每一个类对应一张表
  6. // 可以查询任意表
  7. // 共有三个T,第一个T用来声明类型参数的,后面的两个T才是泛型的实现。
  8. public <T> ArrayList<T> selectAll(Class<T> clazz, String sql, Object... args) {
  9. Connection connection = null;
  10. PreparedStatement preparedStatement = null;
  11. ResultSet resultSet = null;
  12. try {
  13. connection = ConnSql.conncationSql(); // 连接数据库
  14. preparedStatement = connection.prepareStatement(sql); // 预编译sql语句
  15. for (int i = 0; i < args.length; i++) { // 填充占位符
  16. preparedStatement.setObject(i + 1, args[i]);
  17. }
  18. resultSet = preparedStatement.executeQuery(); // 查询数据库
  19. ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); // 获取元数据(列的信息)
  20. int columnLen = resultSetMetaData.getColumnCount(); // 获取有多少列
  21. ArrayList<T> arrayList = new ArrayList<T>();
  22. while (resultSet.next()) {
  23. T t = clazz.getDeclaredConstructor().newInstance(); // 获取对象实例
  24. for (int j = 0; j < columnLen; j++) {
  25. String columnName = resultSetMetaData.getColumnLabel(j + 1); // 获取列名
  26. Object value = resultSet.getObject(j + 1); // 获取值
  27. Field field = clazz.getDeclaredField(columnName); // 通过反射获取私有属性
  28. field.setAccessible(true); // 设置可操作
  29. field.set(t, value);
  30. }
  31. arrayList.add(t);
  32. }
  33. return arrayList;
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. } finally {
  37. ConnSql.closeRes(connection, preparedStatement, resultSet);
  38. }
  39. return null;
  40. }

用到的类

Connection 连接对象

Properties 配置文件操作

PreparedStatement 可以放置sql注入

ResultSet 查询结果的集合