开发者

Can not issue data manipulation statements with executeQuery in java

开发者 https://www.devze.com 2023-01-01 20:50 出处:网络
I\'m trying to insert records in mysql database using java, What do I place in this code so that I could insert records:

I'm trying to insert records in mysql database using java, What do I place in this code so that I could insert records:

String id;
   String name;
   String school;
   String gender;
   String lang;
   Scanner inputs = new Scanner(System.in);



    System.out.println("Input id:");
    id=inputs.next();
    System.out.println("Input name:");
    name=inputs.next();
    System.out.println("Input school:");
    school= in开发者_如何学Pythonputs.next();
    System.out.println("Input gender:");
    gender= inputs.next();
    System.out.println("Input lang:");
    lang=inputs.next();



    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee_record", "root", "MyPassword");
    PreparedStatement statement = con.prepareStatement("insert into employee values('id', 'name', 'school', 'gender', 'lang');");
    statement.executeUpdate();


PreparedStatement statement = con.prepareStatement("insert into employee ('id', 'name', 'school', 'gender', 'lang') values (1,'John','Harvard','male','english');");

or with actual variables

PreparedStatement statement = con.prepareStatement("insert into employee ('id', 'name', 'school', 'gender', 'lang') values ("+id+"'+name+"','"+school+"','"+gender+"','"+lang+"')");

this way it will work, maybe I did typo, haven't tried


It's not a query, it's an update (executeUpdate).

0

精彩评论

暂无评论...
验证码 换一张
取 消