2017-05-06 61 views
0

我在我的应用程序中创建了一个表单场景,并希望在我点击保存按钮时将输入的数据插入到我的SQLite数据库。这里是短代码:JavaFX - 将表单文本框插入到SQLite数据库

@FXML 
private void handleSaveNewShop(ActionEvent event) { 
    String name = shopname.getText(); 
    String adress = streetadress.getText(); 
    String city = cityname.getText(); 
    String state = statename.getText(); 
    String country = countryname.getText(); 
    String zip = zipcode.getText(); 
    String phonept1 = phonecountryid.getText(); 
    String phonept2 = phoneareaid.getText(); 
    String phonept3 = phoneothernumber.getText().toString(); 

    Connection conn = null; 
    Statement stmt = null; 

    try{ 
     Class.forName("org.sqlite.JDBC"); 
     System.out.print("\nConnecting to database..."); 
     conn = DriverManager.getConnection("jdbc:sqlite:FranchiseManagement.sqlite"); 
     System.out.println(" SUCCESS!\n"); 
     /*Tried this 
     String insertToshopList = "INSERT INTO shopList (name, adress, city, state, country, zipcode, phonect, phonearea, phonemain)" + "values(name,adress,city,state,country,zip,phonept1,phonept2,phonept3)"; 
     stmt.executeUpdate(insertToshopList); 
     */ 
     //And This 
     //stmt.executeUpdate("INSERT INTO shopList ('name','adress','city', 'state', 'country', 'zipcode', 'phonect', 'phonearea', 'phonemain') VALUES('"+name+"','"+adress+"','"+city+"'),'"+state+"','"+country+"','"+zip+"','"+phonept1+"','"+phonept2+"','"+phonept3+"'"); 
     conn.commit(); 
     System.out.println(" SUCCESS!\n"); 
     conn.close(); 
    } catch(SQLException se) { 
    se.printStackTrace(); 
    } 
    catch(Exception e) { 
     e.printStackTrace(); 
    } 

但我得到了我提交的零件上的NullPointException。数据库连接似乎没问题,连接。

这里是我的数据库的外观: The Table

The Structure

回答

0

通过删除提交修正,并增加了INSERT到方法以及格式:

@FXML 
private void handleSaveNewShop(ActionEvent event) { 
    String name = shopname.getText(); 
    String adress = streetadress.getText(); 
    String city = cityname.getText(); 
    String state = statename.getText(); 
    String country = countryname.getText(); 
    String zip = zipcode.getText(); 
    String phonept1 = phonecountryid.getText(); 
    String phonept2 = phoneareaid.getText(); 
    String phonept3 = phoneothernumber.getText(); 

    Connection conn; 
    Statement stmt = null; 

    try{ 
     Class.forName("org.sqlite.JDBC"); 
     System.out.print("\nConnecting to database..."); 
     conn = DriverManager.getConnection("jdbc:sqlite:FranchiseManagement.sqlite"); 
     System.out.println(" SUCCESS!\n"); 
     stmt = conn.createStatement(); 
     stmt.executeUpdate("INSERT INTO shopList ('name','adress','city', 'state', 'country', 'zipcode', 'phonect', 'phonearea', 'phonemain') VALUES('"+name+"','"+adress+"','"+city+"','"+state+"','"+country+"','"+zip+"','"+phonept1+"','"+phonept2+"','"+phonept3+"')"); 
     //conn.commit(); 
     System.out.println(" SUCCESS!\n"); 
     conn.close(); 
    } catch (ClassNotFoundException | SQLException e){ 
     Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, e); 
    } 
相关问题