【问题标题】:In core java, While getting input from user and inserting it into msql database, it shows error sql error在核心java中,从用户获取输入并将其插入mysql数据库时,它显示错误sql错误
【发布时间】:2014-08-16 11:44:51
【问题描述】:
import java.util.Scanner;    
import java.sql.*;    
public class Complete {    
public static void main(String args[])    
{    
    System.out.println("Welcome to Your database");    
    try    
    {    
        Scanner keyboard=new Scanner(System.in);    
        System.out.println("Enter you First Name");    
        String f_name=keyboard.nextLine();     
        System.out.println("Enter your Last Name");    
        String l_name=keyboard.nextLine();    
        System.out.println("Welcome"+ " "+ f_name +" "+l_name);    

        System.out.println("Enter your Phone number");    
        String phno=keyboard.nextLine();    

        System.out.println("Enter your email id");    
        String email=keyboard.nextLine();    

        System.out.println("Enter your current city");    
        String city=keyboard.nextLine();    

        System.out.println("Enter you current State");    
        String state=keyboard.nextLine();    

        System.out.println("Enter you pin code");       
        String pin=keyboard.nextLine();    

        System.out.println("------------------------");    
        System.out.println("Your Details are following.");    

        System.out.println("Name :"+ " "+f_name +" "+ l_name);    
        System.out.println("Phone :"+ " "+phno);    
        System.out.println("Email :"+ " "+email);    
        System.out.println("City :"+ " "+city);    
        System.out.println("State :"+ " "+state);    
        System.out.println("PIN code :"+ " "+pin);    




        Class.forName("com.mysql.jdbc.Driver");      

        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3307/checking","root","lenovo");      
        Statement stmt=con.createStatement();      

        stmt.executeUpdate("insert into empdetails(first_name,last_name,phone,email,city,state,pin) values(f_name, l_name, phno, email, city, state, pin)");      
        System.out.println("All Right , We got your entry");    



    }    
    catch(Exception e)    
    {System.out.println(e);}    
    finally{    
        System.out.println("Thanks for using our code");    
    }    
}    
}    

基本上我想从用户获取输入并将其存储在 mysql 数据库中, 在 Eclipse IDE 中执行此代码时,它显示以下错误,

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:“字段列表”中的未知列“f_name”

【问题讨论】:

  • 什么是f_name?你认为它指的是什么?你为什么这么认为? (这适用于 SQL 查询中的所有这些名称。)

标签: java mysql sql jdbc inputstream


【解决方案1】:

SQL 字符串不知道声明的 Java 变量。您不能在 SQL 字符串中引用 Java 变量。

改为使用PreparedStatement? 字符作为值的占位符。在执行之前,调用setStringsetInt(或set[Xyz]对于其他数据类型)来设置这些占位符的值。

【讨论】:

    【解决方案2】:

    保存要插入的值的变量应按如下方式传递(用单引号括起来,因为它们保存字符串值):

    stmt.executeUpdate("insert into empdetails(first_name,last_name,phone,email,city,state,pin) values('" + f_name + "', '" + l_name + "', '" + phno + "', '" + email + "', '" + city + "', '" + state + "', '" + pin +"')"); 
    

    更好的方法是使用PreparedStatement 并将? 参数占位符放在SQL 语句中,您可以将其绑定到您的变量:

    preparedStmt.executeUpdate("insert into empdetails(first_name,last_name,phone,email,city,state,pin) values(?, ?, ?, ?, ?, ?, ?)");
    
    preparedStmt.setString(1, f_name);
    preparedStmt.setString(2, l_name);
    ...
    

    【讨论】:

    • 这可能行得通,但它可能容易受到 SQL 注入攻击。
    【解决方案3】:

    stmt.executeUpdate("插入到

    empdetails(first_name,last_name,phone,email,city,state,pin) 
    
            values(f_name, l_name, phno, email, city, state, pin)");
    

    此 executeUpdate 方法在运行时,需要插入值的 sql 查询。

    但是您在 values() 中直接提到了 java 变量,这对 SQL 来说根本无法理解,因为它希望值在引号中。

    所以最好使用准备好的语句在运行时绑定这些值,如下所示

    preparedStmt.setString(1, f_name);
    preparedStmt.setString(2, l_name);
    preparedStmt.setString(3, phno);
    .......
    
    
    
    preparedStmt.executeUpdate("insert into empdetails(first_name,last_name,phone,email,city,state,pin) values
    
        (?, ?, ?, ?, ?, ?, ?)");
    

    【讨论】:

    • 非常感谢您的宝贵意见。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多