简体   繁体   中英

what's the mistake in this jdbc program

package jdbc;

import java.sql.*;
import java.util.Scanner;

public class Table {

    public static void main(String[] args) {
        String name;
        int age;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter name and age: ");
        name = sc.nextLine();
        age = sc.nextInt();
        try{
            Connection con = DriverManager.getConnection("jdbc:mysql:" +
                                 "//localhost/demo","root","");
            Statement st = con.createStatement();
            //SQL Query to insert user input
            String sql = "insert into tab values(3,"+name+","+age+");";  
            //Execute INSERT
            st.executeUpdate(sql);
            //Dislplay table "tab"
            ResultSet rs = st.executeQuery("select * from tab");
            while(rs.next()){
                System.out.println(rs.getInt(1) + " " + rs.getString(2)"+
                                   " " " + rs.getInt(3));
            }
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

OUTPUT:

Enter name and age: <br>
arpit          //user input<br>
18             //user input<br>
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column<br> 'arpit' in 'field list'<br>
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)<br>
    at<br> sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccesso<br>rImpl.java:62)<br>
    at <br>sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstr<br>uctorAccessorImpl.java:45)<br>
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)<br>
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:400)<br>
    at com.mysql.jdbc.Util.getInstance(Util.java:383)<br>
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:980)<br>
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847)<br>
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)<br>
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)<br>
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)<br>
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2541)<br>
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1604)<br>
    at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1535)<br>
    at jdbc.Table.main(Table.java:20)<br>

Invalid value entered. Use PreparedStatement to set the values in the query. Use column names in the sql which values should be inserted. The primary key is auto generated by default in MySQL, so you don't need to use it. At the end you also don't need to specify the ; . When statement is executed it automatically adds the closing character.

//SQL Query to insert user input
String sql = "insert into tab(name, age) values(?,?)";  
Statement st = con.prepareStatement(sql);

st.setString(1, name);
st.setInt(2, age);

//Execute INSERT
st.executeUpdate();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM