我试了:String sql = " insert into user values ('"+id+"','"+pwdFld+"','"+role+"','"+department+"','"+grade+"','"+ip+"')?useUnicode=true&characterEncoding=utf-8"; 报错: com.mysql.jdbc.exceptions.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 '?useUnicode=true&characterEncoding=utf-8' at line 1 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665) at com.mysql.jdbc.Connection.execSQL(Connection.java:3170) at com.mysql.jdbc.Connection.execSQL(Connection.java:3099) at com.mysql.jdbc.Statement.execute(Statement.java:695) at Server.db$1.run(db.java:42) at java.lang.Thread.run(Thread.java:662)
按要求改了以后,字符编码部分没有错误了,可是却出来了其他的很奇怪的错误: java.sql.SQLException: Incorrect string value: '\xC3\xA6\xC2\x95\xC2\x99...' for column 'role' at row 1 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665) at com.mysql.jdbc.Connection.execSQL(Connection.java:3170) at com.mysql.jdbc.Connection.execSQL(Connection.java:3099) at com.mysql.jdbc.Statement.execute(Statement.java:695) at Server.db$1.run(db.java:42) at java.lang.Thread.run(Thread.java:662)
用winmerge比较一下那里改动了。
这是往数据库中插入数据的部分,大家帮我看下有什么地方不对?
Thread thread = new Thread(new Runnable() {
public void run()
{
Connection con = getConnection();
Statement st;
try {
st =con.createStatement();
String sql = " insert into user values (id,pwdFld,role,department,grade,ip)";
st.execute(sql);
}catch(Exception e){e.printStackTrace();}
finally{
try{
if (con != null) con.close();
}catch(Exception e){e.printStackTrace();}
}
}
});
thread.start();
}
//连接数据库
public static Connection getConnection(){
Connection con = null;
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/sms", "root", "");
System.out.println("数据库连接成功!");
}catch(Exception e){e.printStackTrace();}
return con;
}
?????????
你的id,pwdFld,role,department,grade,ip都是变量吧?
你把它们全引在字符串内就是字符串的内容了啊……
应该这样
String sql = " insert into user values ( " +id+ ","+pwdFld+","+role+","+department+","+grade+","+ip+")";
这样才能将对应的变量值传进字符串啊?……
这样改应该没问题,我写过这种类似的代码。
sql =new String(sql.getBytes( "utf-8" ),"ISO8859_1");
结果报错,我插入的这句话是不是不对啊?
在sql连接串后面加上类似 xxxxx?useUnicode=true&characterEncoding=utf-8
报错:
com.mysql.jdbc.exceptions.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 '?useUnicode=true&characterEncoding=utf-8' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3170)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3099)
at com.mysql.jdbc.Statement.execute(Statement.java:695)
at Server.db$1.run(db.java:42)
at java.lang.Thread.run(Thread.java:662)
连接数据库的语句
jdbc:mysql://localhost:3306/yourdatabase??useUnicode=true&characterEncoding=utf-8"
java.sql.SQLException: Incorrect string value: '\xC3\xA6\xC2\x95\xC2\x99...' for column 'role' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3170)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3099)
at com.mysql.jdbc.Statement.execute(Statement.java:695)
at Server.db$1.run(db.java:42)
at java.lang.Thread.run(Thread.java:662)
role数据是什么东西
JLabel label4= new JLabel("身份:");
label4.setBounds(30, 144, 80, 20);
getContentPane().add(label4);
String[] string0 = {"全体师生", "教师", "学生"};
box1 = new JComboBox(string0);
box1.setBounds(126,144,150,20);
getContentPane().add(box1);box1中获取的值传到了role中role = user.getbox1();
调试下看role插入前的值是否是你需要的
非常非常谢谢你
现在能够插入并正确显示数据了,剩下一个IP地址格式的问题了,我再好好看看。