执行环境:W7 / JDK7 / jdk1.8.0_40 / MS SQL 2012
在SQL有建一个TABLE seats
我想要从JDBC insert into 到seat_num这个栏位里 以 1-1,1-2这要编排下去。下面程序码有去抓另一个table的资料列与行25x20。
import java.sql.*;
public class Seats {
public static void main(String[] args) {
Connection conn = null;
try{
String coonUrl= "jdbc:sqlserver://localhost:1433;databaseName=jdbc";
conn = DriverManager.getConnection(coonUrl, "sa", "passw0rd");
String insStmt = "INSERT INTO playlist VALUES (?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(insStmt);
pstmt.setString(1, "2009-12-25 13:00");
pstmt.setInt(2, 1);
pstmt.setString(3, "A廳");
pstmt = conn.prepareStatement("SELECT seat_row, seat_col FROM m_room where roomid = 'A廳' ");
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
for(int i = 1; i <=rs.getInt("seat_row"); i++ ){
for(int j = 1; j <=rs.getInt("seat_col"); j++){
String seats = "INSERT INTO seats VALUES(?, ?, ?, ?, ?, ?)";
PreparedStatement ppstmt = conn.prepareStatement(seats);
ppstmt.setString(1, "2009-12-25 13:00");
ppstmt.setString(2, "1");
ppstmt.setInt(3, i+j);
ppstmt.setString(4, "0");
ppstmt.setString(5, "Null" );
}
}
}
}catch(SQLException e){
e.printStackTrace();
}finally{
if(conn != null)
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}