所以我正在创建一个程序,我这次尝试做的就是从JTable中复制内容以及其他内容并将它们插入到PostgreSQL表中,事实是,它重复了来自最后一行的数据仅由现有的行数量组成,例如,如果最后一行显示“Cat'并且有5行,它将在PostgreSQL表中插入5行,其中包含“' Cat'在最后一行之前忽略其他人的数据。
try{
Connection conn = null;
PreparedStatement pst = null;
Calendar calendario = GregorianCalendar.getInstance(); // creates a new calendar instance
String hora = Integer.toString(calendario.get(Calendar.HOUR_OF_DAY));
int minutos = calendario.get(Calendar.MINUTE);
int rows=table.getModel().getRowCount();
Class.forName("org.postgresql.Driver").newInstance();
conn = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/ruleta",
"postgres", "123456");
conn.setAutoCommit(false);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String date = sdf.format(new Date());
String queryco = "Insert into tickets(no_ticket,animal,hora,fecha,apuestabsf,ruleta, pagado, premio) values (?,?,?,?,?,?,?,?)";
pst = conn.prepareStatement(queryco);
for(int row = 0; row<rows; row++)
{
String jugada = (String)table.getValueAt(row, 0);
String ruleta = (String)table.getValueAt(row, 1);
String apuesta = (String)table.getValueAt(row, 2);
pst.setInt(1, variable.ticket);
pst.setString(2, variable.animal);
pst.setString(3, hora + ":" + minutos);
pst.setString(4, date);
pst.setInt(5, Integer.parseInt(textField_1.getText()));
if(chckbxNewCheckBox.isSelected() == true) pst.setString(6, selec1);
if(chckbxNewCheckBox_1.isSelected() == true) pst.setString(6, selec2);
if(chckbxNewCheckBox_2.isSelected() == true) pst.setString(6, selec3);
if(chckbxNewCheckBox_3.isSelected() == true) pst.setString(6, selec4);
if(chckbxNewCheckBox_4.isSelected() == true) pst.setString(6, selec5);
if(chckbxNewCheckBox_5.isSelected() == true) pst.setString(6, selec6);
if(chckbxNewCheckBox_6.isSelected() == true) pst.setString(6, selec7);
if(chckbxNewCheckBox_7.isSelected() == true) pst.setString(6, selec8);
pst.setString(7, "No decidido");
pst.setInt(8, Integer.parseInt(textField_1.getText()) * 30);
pst.addBatch();
}
pst.executeBatch();
conn.commit();
}
catch(Exception e){
System.err.println(e);
}
答案 0 :(得分:0)
您从表中检索一些数据:
String jugada = (String)table.getValueAt(row, 0);
String ruleta = (String)table.getValueAt(row, 1);
String apuesta = (String)table.getValueAt(row, 2);
但无法将它们插入数据库的任何位置。