JDBC更新查询错误 [英] JDBC Update query error

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

问题描述

我无法使用where子句在JDBC上执行更新查询.我想通过串联一些变量来执行查询.

I can't execute the update query on JDBC with where clause. I want to execute the query by concatenating some variables.

protected void updateEmail(String Email, String Username) {
    try {

        conn = DriverManager.getConnection("jdbc:mysql://localhost/project", "root", "");

        String query = "update access set email=" + Email + " where username=" + Username;

        Statement st = conn.createStatement();
        st.executeUpdate(query);

    } catch (SQLException ex) {
        System.out.println(ex);
    } finally {
        try {
            conn.close();
        } catch (SQLException ex) {

        }
    }
}

它说:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'bhusal1' in 'where clause'

推荐答案

字符串应位于两个引号'name'之间,而不是必须使用

String should be between two quotes 'name' instead of your way you have to use PreparedStatement instead to avoid syntax error or SQL Injection :

String query = "update access set email=? where username=?";
PreparedStatement ps = con.prepareStatement(query);

ps.setString(1, email);
ps.setString(2, name);
ps.executeUpdate();

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

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