2013-11-04 20 views
0

使用netbeans和mysql我創建了一個表,我希望用戶能夠使用java向它添加數據。有人可以舉例說明如何將新客戶名稱添加到具有字段名稱的表名客戶端。Netbeans mysql表

這會自動把它,但我需要的用戶名添加到表

public static void insertClients() throws SQLException { 
    try { 
     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
    } catch (Exception E) { 
     System.err.println("Unable to load driver."); 
     E.printStackTrace(); 
    } 

    try { 
     Connection con = DriverManager.getConnection(
         "jdbc:mysql://localhost:3306/clients","root","pass"); 
     con.setAutoCommit(false); 
     Statement statement = con.createStatement(); 
     String newClient = "insert into client values(John);"; 
     statement.executeUpdate(newName); 
     System.out.println("Client added"); 
     con.commit(); 
     con.close(); 
+0

你需要引用你的名字,並命名你插入的列。但更好地使用*準備好的語句*。 –

回答

-1

您可以按照有關如何使用預處理語句將數據插入到表官方documentation

幾乎不會花你一個小時來嘗試那裏給出的例子。

希望它有幫助。