【问题标题】:Wanting to insert for the first time in database and getting an error想第一次在数据库中插入并收到错误
【发布时间】:2013-06-01 14:33:18
【问题描述】:

这是我的代码

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

【问题讨论】:

    标签: java mysql jdbc insert


    【解决方案1】:

    desc 是保留的 SQL 关键字(用于order by foo desc)。更改您命名为“desc”的列的名称(或使用反引号对其进行转义,但我会更改名称以使其更容易)

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2018-11-02
        • 2013-05-07
        • 2017-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-09
        相关资源
        最近更新 更多