日期:2014-05-19  浏览次数:20972 次

插入数据发生异常:You have an error in your SQL syntax; check the manual that corresponds
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
本人菜鸟级别 7_7


import java.sql.DriverManager;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.*;

public class Addvalue {
private String driver = "com.mysql.jdbc.Driver";
private String url ="jdbc:mysql://127.0.0.1:3306/luke";
private String user = "root";
private String pass = "671792";
// private String sql ="insert into jdbc_test values(?,?)";
private JFrame jf = new JFrame("添加数据窗口");
private JLabel us = new JLabel("请输入用户名");
private JTextField use = new JTextField(15);
private JLabel ps = new JLabel("请输入密码");
private JPasswordField pas = new JPasswordField(15);
private JLabel pps = new JLabel("确认密码");
private JPasswordField pas2 = new JPasswordField(15);
private JButton inse = new JButton("确定添加数据");
private JButton reset = new JButton("重置信息");
/**
* 创建添加窗口
*/
public void init() throws Exception {
Class.forName(driver);
jf.setLayout(new GridLayout(4, 2, 1, 1));
jf.add(us);
jf.add(use);
jf.add(ps);
jf.add(pas);
jf.add(pps);
jf.add(pas2);
jf.add(reset);
jf.add(inse);
jf.setBounds(500, 400, 260, 152);
// jf.setSize(260, 152);
jf.setVisible(true);
inse.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (use.getText().length() == 0 ) {
JOptionPane.showMessageDialog(jf, "请检查输入");
}
else if(use.getText().length()!=0 && pas.getPassword()!=null)

insert("use","pas");
JOptionPane.showMessageDialog(jf, "添加数据成功");

use.setText("");
pas.setText("");
pas2.setText("");
}}
});
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
use.setText("");
pas.setText("");
pas2.setText("");
}
});
}

public void insert(String uuu, String ppp) {
char[] s = null;
s = pas.getPassword();
uuu = use.getText();
ppp = new String(s);
String sq ="insert into zhuce values(?,?)";
// String sq ="insert into jdbc_test values(uuu,ppp)";
try (Connection conn = DriverManager.getConnection(url, user, pass);
PreparedStatement pstmt = conn
.prepareStatement(sq)) {
// 获取密码域的内容
pstmt.setString(1, uuu);
pstmt.setString(2, ppp);
// pstmt.executeQuery(sq);
pstmt.executeUpdate(sq) ;
if (pas.getPassword() == pas2.getPassword() ) {
insert("uuu", "ppp");
// JOptionPane.showMessageDialog(jf, "添加数据成功");
return;
}
} catch (SQLException ex) {
System.out.println("插入数据发生异常:" + ex.getMessage());
}
}

public static void main(String[] args) throws Exception {
(new Addvalue()).init();
}
}

------解决方案--------------------
insert into table_name(c_1, c_2, c_3) values(v_1, v_2, v_3); 先看下你的数据库表到底有几个字段,如果超过两个,记得写上你的字段,如果只有两个,可以忽略...还有,你的代码有很多问题..我也懒得看了,发现个严重的,两个String是否相等,是这样判断的, s1.equals(s2)
<