StringBuffer sql=new StringBuffer("insert into " );作用是什么
时间: 2024-02-12 22:29:10 浏览: 25
这段代码的作用是创建一个 StringBuffer 对象,并将字符串 "insert into " 添加到它的缓冲区中。通常这段代码是用于拼接 SQL 语句的,通过在 "insert into " 后面添加表名和字段名等信息,最终拼接成完整的 SQL 语句。例如可以在后面添加 "tableName (column1, column2) values (?, ?)",然后通过设置占位符的值,执行 SQL 插入操作。
相关问题
public int insert(T t) throws Exception{ Connection connection=null; PreparedStatement ps=null; StringBuffer sql=new StringBuffer("insert into " ); //得到T的反射类 Class<?> aClass=t.getClass(); //获取类上注解 TableName tableName = aClass.getAnnotation(TableName.class); if (tableName==null){ throw new RuntimeException("未使用注解标记表名"); } //获取表名 String name = tableName.name(); sql.append(name+ " values"); try { List<Object> values = new ArrayList<Object>(); Field[] declaredFields = aClass.getDeclaredFields(); for (Field field : declaredFields) { field.setAccessible(true); Object v1 = field.get(t); values.add("'" + v1 + "'"); } String replace = values.toString().replace("[", "(").replace("]", ")"); sql.append(replace); connection = ConnectionFactory.getConnection(); ps = connection.prepareStatement(sql.toString()); int i = ps.executeUpdate(); return i; }catch (Exception e){ e.printStackTrace(); }finally { ConnectionFactory.close(null,ps,connection); } return 0; }
这段代码是用来实现数据库插入操作的,其中的T是一个泛型参数,可以代表任意一个Java类,而该类必须使用了@TableName注解标记了数据库表名。这个方法首先通过反射获取到T的Class对象,然后从该Class对象中获取@TableName注解并获取表名,接着通过反射获取到该类中所有的Field对象,遍历每个Field对象并获取其值,最后构造出一个SQL语句并执行数据库插入操作。需要注意的是,这段代码并没有对SQL注入进行防范,如果插入的数据中包含特殊字符,可能会导致SQL注入攻击。
public int add(Connection con,SchoolClass schoolClass)throws Exception{ String sql="insert into t_school_class values(null,?,?)"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, schoolClass.getClassName()); System.out.println(schoolClass.getClassName()); pstmt.setString(2, schoolClass.getCalssDesc()); return pstmt.executeUpdate(); } /** * 查询班级集合 * @param con * @param schoolClass * @return * @throws Exception */ public ResultSet list(Connection con,SchoolClass schoolClass)throws Exception{ StringBuffer sb=new StringBuffer("select * from t_school_class"); if(StringUtil.isNotEmpty(schoolClass.getClassName())){ sb.append(" and className like '%"+schoolClass.getClassName()+"%'"); } PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where")); return pstmt.executeQuery(); } /** * 删除班级 * @param con * @param id * @return * @throws Exception */ public int delete(Connection con,String id)throws Exception{ String sql="delete from t_school_class where id=?"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, id); return pstmt.executeUpdate(); } /** * 更新班级 * @param con * @param schoolClass * @return * @throws Exception */ public int update(Connection con,SchoolClass schoolClass)throws Exception{ String sql="update t_school_class set className=?,classDesc=? where id=?"; PreparedStatement pstmt=con.prepareStatement(sql); pstmt.setString(1, schoolClass.getClassName()); pstmt.setString(2, schoolClass.getCalssDesc()); pstmt.setInt(3, schoolClass.getId()); return pstmt.executeUpdate(); } }
这段代码看起来像是一个 Java 类中的一些数据库操作方法,这些方法分别是添加班级、查询班级集合、删除班级和更新班级信息。这些方法都需要一个数据库连接对象 Connection 和一个 SchoolClass 类型的参数。其中,添加班级方法使用了 PreparedStatement 预编译 SQL 语句,查询班级集合方法使用了 StringBuffer 拼接 SQL 语句,并使用了 StringUtil 工具类的方法判断字符串是否为空,删除班级方法使用了 PreparedStatement 预编译 SQL 语句并传入一个 ID 参数,更新班级信息方法同样使用了 PreparedStatement 预编译 SQL 语句,并传入一个 SchoolClass 对象的参数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)