无法在java中使用executeQuery发出数据操作语句

时间:2010-06-03 07:42:24

标签: java mysql

我正在尝试使用java在mysql数据库中插入记录, 我在此代码中放置了什么以便我可以插入记录:

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= inputs.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();

2 个答案:

答案 0 :(得分:1)

这不是查询,而是更新(executeUpdate)。

答案 1 :(得分:1)

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

或实际变量

PreparedStatement statement = con.prepareStatement("insert into employee ('id', 'name', 'school', 'gender', 'lang') values ("+id+"'+name+"','"+school+"','"+gender+"','"+lang+"')");
这样它会起作用,也许我做错了,没试过