tags:

views:

234

answers:

2

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= 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();
+1  A: 

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

extraneon
thanks, tried statement.executeUpdate but there's something wrong here: values('id', 'name' ...)
+1  A: 
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

Xorty
thanks man, but without entering the column it works fine
yes you don't have to specify values, but if you don't it means you want to use all of them.Anway, glad I could hlep
Xorty