日期:2014-05-20  浏览次数:20893 次

向数据库添加客户信息SQL语句出错
源代码主要是为了实现一个添加客户信息的图形界面,如下:

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);
  }
  }
   
}

求大神帮帮忙,万分感谢。。


------解决方案--------------------
我记得我写sql的时候,都把字符串值用''给包起来,如:
Java code

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

------解决方案--------------------