今天,我尝试在mysql插入数据后获取自增字段的值。
参考:百度文心一言的回复。
public static int insertTimu(String timuLeixing, String timuWenti, String timuDaan, String timuXuanxiang) throws Exception { int res = 0; Connection connection = util.DBUtil.getConnection(); PreparedStatement preparedStatement = null; String sql = "INSERT INTO timu (timuLeixing,timuWenti,timuDaan,timuXuanxiang) VALUES (?,?,?,?)"; preparedStatement = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS); preparedStatement.setString(1, timuLeixing); preparedStatement.setString(2, timuWenti); preparedStatement.setString(3, timuDaan); preparedStatement.setString(4, timuXuanxiang); System.out.println(preparedStatement.toString()); preparedStatement.executeUpdate(); ResultSet rs = preparedStatement.getGeneratedKeys(); if (rs.next()) { res = rs.getInt(1); System.out.println("Inserted ID: " + res); } return res; }
标签:2024.1,preparedStatement,String,23,res,setString,rs,笔记,timuDaan From: https://www.cnblogs.com/zhangxutong/p/17982822