想要第一次在数据库中插入并收到错误

时间:2013-06-01 14:33:18

标签: java mysql jdbc insert

所以这是我的代码

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/test"; 
//ResultSet rs = null;
PreparedStatement psmnt = null;
//FileInputStream fis;

connection = DriverManager.getConnection(connectionURL,"root","123");
psmnt = connection.prepareStatement("insert into Table1 (name, desc, r,g,b," +
    "varR,varG,varB,skewnessR,skewnessG,skewnessB)values(?,?,?,?,?,?,?,?,?,?,?)");
final String text2 = setName.getText();
final String text3=setDesc.getText();
psmnt.setString(1,text2);
psmnt.setString(2,text3);
psmnt.setString(3,stringMeanR);
psmnt.setString(4,stringMeanG);
psmnt.setString(5,stringMeanB);
psmnt.setString(6,stringVarianceR);
psmnt.setString(7,stringVarianceG);
psmnt.setString(8,stringVarianceB);
psmnt.setString(9,stringSkewnessR);
psmnt.setString(10,stringSkewnessG);
psmnt.setString(11,stringSkewnessB);

//fis = new FileInputStream(f);

//psmnt.setBinaryStream(2, (InputStream)fis, (int)(f.length()));
int s = psmnt.executeUpdate();
psmnt.close();
connection.close();
if(s>0) {
    System.out.println("Uploaded successfully !");
}
else {
    System.out.println("Unsucessfull to upload image.");
}                      

问题是我收到以下错误

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 'desc, r,g,b,varR,varG,varB,skewnessR,skewnessG,skewnessB)values('','','105.59 , ' at line 1

2 个答案:

答案 0 :(得分:4)

desc是一个保留的SQL关键字(在order by foo desc中使用)。更改名为“desc”的列的名称(或使用反引号将其转义,但我会更改名称以使其更容易)

答案 1 :(得分:1)

desc是SQL中用于描述SQL表的保留关键字。因此,如果您的列名称为“description”,那么它应该是“description”;不是'desc',因为desc在MySQL中保留用于描述表本身或显示用于创建表的SQL代码。