gpt4 book ai didi

java - 无法将数据从 JTable 插入数据库

转载 作者:行者123 更新时间:2023-11-30 23:31:01 25 4
gpt4 key购买 nike

我正在编写一个程序,将数据从 JTable 插入到数据库中。我没有收到任何错误或异常。甚至对象也被正确填充。但不知道为什么 executeUpdate 语句会失败。下面是我的代码。

import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;

public class Table extends JFrame implements ActionListener, TableModelListener {

private JTable table;
private JButton addItem, cancel, save, load, search;
DefaultTableModel tableModel;
Object[] columnNames = new Object[]{
"Item No", "ItemName","UnitPrice", "Qty","TotalPrice","Supplier","Remarks"};

public Table() {
getContentPane().setLayout( new FlowLayout() );

tableModel = new DefaultTableModel(columnNames, 0);
tableModel.addTableModelListener( this );

table = new JTable(tableModel);

JScrollPane scrollPane = new JScrollPane(table);
table.setPreferredScrollableViewportSize(new Dimension(600, 400));
getContentPane().add(scrollPane);

addItem = new JButton( "Add" );
addItem.addActionListener( this );
getContentPane().add( addItem );

save = new JButton( "Save" );
save.addActionListener( this );
getContentPane().add( save );

cancel = new JButton( "Cancel" );
cancel.addActionListener( this );
getContentPane().add( cancel );
setVisible( true );
setSize( 640, 500 );
}

public void tableChanged( TableModelEvent e ) {

}

public void actionPerformed( ActionEvent evt ) {
int row = table.getSelectedRow();
int column = table.getSelectedColumn();
if(evt.getSource()== addItem)
{
tableModel.addRow(new Object[]{"","","","","","",""});
}
else if ( evt.getSource() == save )
saveCustomers();
}

private void saveCustomers() {
PreparedStatement pstm = null;
ResultSet rs;
int index=1;
int count=table.getRowCount();

try{
Class.forName("com.mysql.jdbc.Driver");
Connection connect =DriverManager.getConnection("jdbc:mysql://localhost/restaurant","root","root");
for(int i=0;i<count;i++){
Object obj1 = GetData(table, i, 0);
Object obj2 = GetData(table, i, 1);
Object obj3 = GetData(table, i, 2);
Object obj4 = GetData(table, i, 3);
Object obj5 = GetData(table, i, 4);
Object obj6 = GetData(table, i, 5);
Object obj7 = GetData(table, i, 6);

int value1= Integer.parseInt(obj1.toString());
System.out.println(value1);
String value2=obj2.toString();
double value3=Double.parseDouble(obj3.toString());
System.out.println(value2);
double value4=Double.parseDouble(obj4.toString());
System.out.println(value3);
double value5=Double.parseDouble(obj5.toString());
System.out.println(value5);
String value6=obj6.toString();
System.out.println(value6);
String value7=obj7.toString();
System.out.println(value7);
pstm=connect.prepareStatement("insert into stock values(?,?,?,?,?,?,?)");
System.out.println("Preperation");
pstm.setInt(1,value1);
pstm.setString(2,value2);
pstm.setDouble(3,value3);
pstm.setDouble(4, value4);
pstm.setDouble(5,value5);
pstm.setString(6, value6);
pstm.setString(7, value7);

index++;
}
System.out.println("execute");
pstm.executeUpdate();
System.out.println("saved Successfully");
}
catch(Exception e){}
}


public Object GetData(JTable table, int row_index, int col_index){
return table.getModel().getValueAt(row_index, col_index);
}



public static void main ( String[] args ) {
Table frm = new Table();
frm.setVisible(true);
frm.setSize( 640, 500 );
frm.setResizable(false);
WindowQuitter wquit = new WindowQuitter();
frm.addWindowListener( wquit );
}
}

class WindowQuitter extends WindowAdapter {
public void windowClosing( WindowEvent e ) {
System.exit( 0 );
}
}

最佳答案

我不确定,但我认为你的连接 url 是错误的,试试这个

Connection con = ( Connection ) DriverManager.getConnection( "jdbc:mysql://localhost:3306/yourdatabasename", "yourmysqlid", "yourmysqlpassword" );

其中 3306 是本地主机上 mysql 的默认端口号。

关于java - 无法将数据从 JTable 插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10659310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com