首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查询问题-在JDBC中插入(双引号,单引号)

问查询问题-在JDBC中插入(双引号,单引号)
EN

Stack Overflow用户
提问于 2020-10-01 16:40:37
回答 1查看 347关注 0票数 1

我有一个愚蠢的问题,我无法解决。我正在学习Java,而且我还是个新手。我的案例是:

//将人员添加到数据库中

代码语言:javascript
复制
public static void aggiungiPersona(int id, String nome, String cognome, int anni, String sesso, 
                                  String indirizzo, String numTel, String email) {

    try {
             // create query
        String query = String.join("", "insert into persone (id, nome, cognome, anni, sesso, indirizzo, numTel, email) VALUES (",
                
                Integer.toString(id), ", '",
                nome, "', '", 
                cognome, "', ", 
                Integer.toString(anni), ", '",
                sesso, "', '", 
                indirizzo, "', '",
                numTel, "', '",
                email, "', ",
                 ")"

                );    

我知道问题出在引号或双引号中,但是在哪里呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-01 16:50:56

你应该在这里使用一个准备好的语句来处理你的文字值的正确转义:

代码语言:javascript
复制
String sql = "INSERT INTO persone (id, nome, cognome, anni, sesso, indirizzo, numTel, email) ";
sql += "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, Integer.toString(id));   // use ps.setInt(1, id) if id be integer column
ps.setString(2, nome);
ps.setString(3, cognome);
ps.setString(4, Integer.toString(anni)); // use ps.setInt(4, anni) for anni integer column
ps.setString(5, sesso);
ps.setString(6, indirizzo);
ps.setString(7, numTel);
ps.setString(8, email);
int row = ps.executeUpdate();
System.out.println(row);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64151808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档