mysql 批量插数据到数据库以及返回插入生成的id号
2013-10-19 11:26
886 查看
首先是插入一条数据到数据库,并返回插入时的id号,数据的设计 id号的是自动递增的
public static int saveHotel(Hotel hotel) {
PreparedStatement pstmt = null;
Connection conn = DB.getConnection();
try {
String sql = "insert into t_hotel values(null , ?,? ,? ,?,? ,? ,?,?,?)";
//插入数据时返回的id号
pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1, hotel.getName());
pstmt.setString(2, hotel.getPassword());
pstmt.setString(3, hotel.getAddress());
pstmt.setString(4, hotel.getPhone());
pstmt.setInt(5, hotel.getPrice());
pstmt.setString(6, hotel.getImg());
pstmt.setString(7, hotel.getCuisine());
pstmt.setString(8, hotel.getArea());
pstmt.setString(9, hotel.getStreet());
pstmt.executeUpdate();
//获得返回的id号
ResultSet rs = pstmt.getGeneratedKeys();
rs.next();
int returnKey = rs.getInt(1);
return returnKey ;
} catch (SQLException e) {
e.printStackTrace();
return 0 ;
} finally {
DB.closepStmt(pstmt);
DB.closeConn(conn);
}
}
批量插入数据并返回id号
public static ResultSet saveHotel(List<hotel> hotels) {
PreparedStatement pstmt = null;
Connection conn = DB.getConnection();
try {
String sql = "insert into t_hotel values(null , ?,? ,? ,?,? ,? ,?,?,?)";
//插入数据时返回的id号
pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
for(Hotel hotel :hotels){
pstmt.setString(1, hotel.getName());
pstmt.setString(2, hotel.getPassword());
pstmt.setString(3, hotel.getAddress());
pstmt.setString(4, hotel.getPhone());
pstmt.setInt(5, hotel.getPrice());
pstmt.setString(6, hotel.getImg());
pstmt.setString(7, hotel.getCuisine());
pstmt.setString(8, hotel.getArea());
pstmt.setString(9, hotel.getStreet());
pstmt.addBatch();
}
pstmt.executeBatch();
//获得返回的id号,是一个set
ResultSet keyRs =
pstmt.getGeneratedKeys();
} catch (SQLException e) {
e.printStackTrace();
return 0 ;
} finally {
DB.closepStmt(pstmt);
DB.closeConn(conn);
}
return keyRs ;
}
public static int saveHotel(Hotel hotel) {
PreparedStatement pstmt = null;
Connection conn = DB.getConnection();
try {
String sql = "insert into t_hotel values(null , ?,? ,? ,?,? ,? ,?,?,?)";
//插入数据时返回的id号
pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1, hotel.getName());
pstmt.setString(2, hotel.getPassword());
pstmt.setString(3, hotel.getAddress());
pstmt.setString(4, hotel.getPhone());
pstmt.setInt(5, hotel.getPrice());
pstmt.setString(6, hotel.getImg());
pstmt.setString(7, hotel.getCuisine());
pstmt.setString(8, hotel.getArea());
pstmt.setString(9, hotel.getStreet());
pstmt.executeUpdate();
//获得返回的id号
ResultSet rs = pstmt.getGeneratedKeys();
rs.next();
int returnKey = rs.getInt(1);
return returnKey ;
} catch (SQLException e) {
e.printStackTrace();
return 0 ;
} finally {
DB.closepStmt(pstmt);
DB.closeConn(conn);
}
}
批量插入数据并返回id号
public static ResultSet saveHotel(List<hotel> hotels) {
PreparedStatement pstmt = null;
Connection conn = DB.getConnection();
try {
String sql = "insert into t_hotel values(null , ?,? ,? ,?,? ,? ,?,?,?)";
//插入数据时返回的id号
pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
for(Hotel hotel :hotels){
pstmt.setString(1, hotel.getName());
pstmt.setString(2, hotel.getPassword());
pstmt.setString(3, hotel.getAddress());
pstmt.setString(4, hotel.getPhone());
pstmt.setInt(5, hotel.getPrice());
pstmt.setString(6, hotel.getImg());
pstmt.setString(7, hotel.getCuisine());
pstmt.setString(8, hotel.getArea());
pstmt.setString(9, hotel.getStreet());
pstmt.addBatch();
}
pstmt.executeBatch();
//获得返回的id号,是一个set
ResultSet keyRs =
pstmt.getGeneratedKeys();
} catch (SQLException e) {
e.printStackTrace();
return 0 ;
} finally {
DB.closepStmt(pstmt);
DB.closeConn(conn);
}
return keyRs ;
}
相关文章推荐
- php插入mysql数据返回id的方法
- mysql+mybatis插入一条数据后返回主键ID
- mybaties+mysql:插入数据,返回自增长的id
- mybatis mysql 插入对象返回数据主键id
- mysql插入数据后返回自增ID的方法
- MySQL插入数据后返回自增id的方法
- mysql插入数据后返回自增ID的方法
- php插入mysql数据返回id
- 数据库插入数据返回当前主键ID值方法
- C#中返回最后一次插入数据的id(数据库:sql server)
- mysql插入数据后返回自增ID的方法,last_insert_id(),selectkey
- mysql插入数据后返回自增ID
- Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer 关于Newtonsoft.Json,LINQ to JSON的一个小demo mysql循环插入数据、生成随机数及CONCAT函数 .NET记录-获取外网IP以及判断该IP是属于网通还是电信 Guid的生成和数据修整(去除空格和小写字符)
- Java 批量插入数据库(MySQL)数据
- mysql 插入数据后返回当前的自增ID方法
- MySQL入门(创建数据库以及插入,修改,查询数据)
- java,C#插入数据到oracle11g数据库并返回其id
- 1.读写分离(主数据库写 可分拆副数据库读);2.垂直分库(将系统按业务进行了垂直拆分主数据库);3.水平分主库(ID生成算法决定了如何拆分数据,以及增删改查)
- 数据库 从 临时表 批量插入 并且 生成id 的问题
- mysql插入一条数据,返回其对应的id