增加
package test;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.sql.*;import java.util.Properties;public class ConnectionTest { public static void main(String[] args) throws Exception { BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("E:\\IdeaProjects\\Demo\\src\\test\\database.properties")); Properties properties = new Properties(); properties.load(inputStream); String url = properties.getProperty("url"); String user = properties.getProperty("user"); String password = properties.getProperty("password"); String driverStr = properties.getProperty("driver"); Class.forName(driverStr); Connection driverManager = DriverManager.getConnection(url, user, password); //========================================================================== // ?表示占位符 String sql = "insert into student(sid,sname,sage,classid) values(?,?,?,?)"; PreparedStatement ps = driverManager.prepareStatement(sql); ps.setInt(1,5); ps.setString(2,"ps"); ps.setInt(3,19); ps.setInt(4,4); ps.execute(); //执行sql语句 }}
增 删 改 只是sql语句和占位符不一样 可以写成通用方法
// 传入sql语句 和 占位符的真实值 public void update(String sql, Object... args) { Connection connection = null; PreparedStatement preparedStatement = null; try { connection = ConnSql.conncationSql(); preparedStatement = connection.prepareStatement(sql); for (int i = 0; i < args.length; i++) { // 循环填充占位符 preparedStatement.setObject(i + 1, args[i]); } // 执行sql语句 preparedStatement.execute(); } catch (Exception e) { e.printStackTrace(); }finally { // 关闭 ConnSql.close(connection,preparedStatement); } }
查询
- getColumnName 获取列名
- getColumnLabel 获取列的别名
- 注意: 使用getColumnLabel时,列没有别名 会使用列名,可以解决数据库中表的列名,跟java中类属性名不一致的问题
// 获取元数据 ResultSetMetData类中的方法可以查看表中列的信息ResultSetMetData resultMetData = res.getMetData//==========================================================================// 利用反射的技术 给对象赋值// 每一个类对应一张表// 可以查询任意表 // 共有三个T,第一个T用来声明类型参数的,后面的两个T才是泛型的实现。 public <T> ArrayList<T> selectAll(Class<T> clazz, String sql, Object... args) { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { connection = ConnSql.conncationSql(); // 连接数据库 preparedStatement = connection.prepareStatement(sql); // 预编译sql语句 for (int i = 0; i < args.length; i++) { // 填充占位符 preparedStatement.setObject(i + 1, args[i]); } resultSet = preparedStatement.executeQuery(); // 查询数据库 ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); // 获取元数据(列的信息) int columnLen = resultSetMetaData.getColumnCount(); // 获取有多少列 ArrayList<T> arrayList = new ArrayList<T>(); while (resultSet.next()) { T t = clazz.getDeclaredConstructor().newInstance(); // 获取对象实例 for (int j = 0; j < columnLen; j++) { String columnName = resultSetMetaData.getColumnLabel(j + 1); // 获取列名 Object value = resultSet.getObject(j + 1); // 获取值 Field field = clazz.getDeclaredField(columnName); // 通过反射获取私有属性 field.setAccessible(true); // 设置可操作 field.set(t, value); } arrayList.add(t); } return arrayList; } catch (Exception e) { e.printStackTrace(); } finally { ConnSql.closeRes(connection, preparedStatement, resultSet); } return null; }
用到的类
Connection 连接对象
Properties 配置文件操作
PreparedStatement 可以放置sql注入
ResultSet 查询结果的集合