向数据库添加客户信息SQL语句出错

yoshikis 2012-05-29 05:22:32
源代码主要是为了实现一个添加客户信息的图形界面,如下:

import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class Customer extends JFrame implements ActionListener{
public static void main(String args[]){
Customer customer=new Customer();
}
JTable table;
Object a[][];
Object name[]={"客户编号","客户姓名","联系电话","送货地址","信用度","VIP","邮编"};
JButton Comfirm,Cancel;
JTextField inputNumber;
int initNumber=1;
JPanel pSouth,pNorth;
Customer(){
Comfirm=new JButton("确定");
Cancel=new JButton("取消");
inputNumber=new JTextField(10);
Comfirm.addActionListener(this);
Cancel.addActionListener(this);
inputNumber.addActionListener(this);
pSouth=new JPanel();
pNorth=new JPanel();
pNorth.add(new JLabel("输入客户数,回车确定"));
pNorth.add(inputNumber);
pSouth.add(Comfirm);
pSouth.add(Cancel);
add(pSouth,BorderLayout.SOUTH);
add(pNorth,BorderLayout.NORTH);
add(new JScrollPane(table),BorderLayout.CENTER);
setBounds(300,400,680,250);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

}
public void actionPerformed(ActionEvent e){
if(e.getSource()==inputNumber){
initNumber=Integer.parseInt(inputNumber.getText());
a=new Object[initNumber][7];
for(int i=0;i<initNumber;i++){
for(int j=0;j<7;j++)
{

a[i][j]=" ";
}
}
table=new JTable(a,name);
table.setRowHeight(20);
getContentPane().removeAll();
add(new JScrollPane(table),BorderLayout.CENTER);
add(pSouth,BorderLayout.SOUTH);
add(pNorth,BorderLayout.NORTH);
validate();
}
else if(e.getSource()==Comfirm)
{

for(int i=0;i<initNumber;i++)
{
int j=0;
try{

Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost/coldwine";
Connection con=DriverManager.getConnection(url,"root","");
Statement st=con.createStatement();
String sql="insert into customer(ID,name,phone,address,credit,IfVip,zipcode) values("+a[i][j]+","+a[i][j+1]+","+a[i][j+2]+","+a[i][j+3]+","+a[i][j+4]+","+a[i][j+5]+","+a[i][j+6]+")"; //主要出错在这里,a[i][j]不行吗,怎么办,求修改下代码
st.executeUpdate(sql);
}
catch (Exception ex)
{
System.out.println("Error : " + ex.toString());
}
}
}
else
{
System.exit(0);
}
}

}

求大神帮帮忙,万分感谢。。
...全文
187 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yoshikis 2012-05-29
  • 打赏
  • 举报
回复
我知道哪里错了,感谢提醒。。结贴。
七_号 2012-05-29
  • 打赏
  • 举报
回复
a[i][j+6] 为""
建议 你把每个值用单引号 引起来。
yoshikis 2012-05-29
  • 打赏
  • 举报
回复
错误信息提示为:Error : com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '???, 3305425, ??, 1, Y, )' at line 1
酒比花香 2012-05-29
  • 打赏
  • 举报
回复
直接输出 sql,放在数据库里查查不就行了
wang7535067 2012-05-29
  • 打赏
  • 举报
回复
什么错误,ID一般是自动生成,不用插入的。
Think.Chen 2012-05-29
  • 打赏
  • 举报
回复
1楼的试试啊,你的字符串拼接有问题
benniaopiaoling 2012-05-29
  • 打赏
  • 举报
回复
错误信息帖出来
李狗蛋52635 2012-05-29
  • 打赏
  • 举报
回复
我记得我写sql的时候,都把字符串值用''给包起来,如:

String sql = ... VALUES('" + str1 + "', '" + str2 + ...


不知道是不是这个原因呢?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧