MySQL Java更新语法 [英] MySQL Java Update Syntax

查看:82
本文介绍了MySQL Java更新语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是尝试在应用程序中使用更新,但我不能.在控制台上,此mysql命令有效,但在这里无效.

I just trying to use a update in my application, but i just can't. A the console, this mysql command works, but here, doesn't.

好吧,我在程序中使用了它:

Well, I use this in my program:

    conexao = poolMySQL.connect();
    final String sql = "UPDATE professor  SET codlocal = ?  WHERE codprof = ?";

    pstmt = conexao.prepareStatement(sql);


    pstmt.setInt(1, local);
    pstmt.setInt(2, id);
    try {
        pstmt.executeUpdate(sql);
    } catch (SQLException ex) {
        Logger.getLogger(ProfessorDAO.class.getName()).log(Level.SEVERE, null, ex);
    }

请记住,codlocal是另一个名为Localidade的表的外键.我看到了一些有关加入的示例,但我无法想象这对我如何起作用.

Just reminding, codlocal is a foreign key from another table, named Localidade. I saw some examples about join, but i just can't imagine how this can work for me.

我明白了:

    Grave: null
    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 '?  WHERE codprof = ?' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.Util.getInstance(Util.java:384)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2562)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1664)
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1583)
    at DAO.ProfessorDAO.atualizaLocalidade(ProfessorDAO.java:153)
    at DAO.Main.main(Main.java:23)

那我该怎么办?

推荐答案

您必须使用pstmt.executeUpdate();代替pstmt.executeUpdate(sql);

You have to use pstmt.executeUpdate(); in place of pstmt.executeUpdate(sql);

这篇关于MySQL Java更新语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆