簡體   English   中英

如何使用Java將數據從4個文本字段發送到Netbeans中的Derby數據庫

[英]How do I send data from 4 text fields to a Derby Database in Netbeans using Java

我有一個注冊頁面,可以在4個文本字段中輸入客戶信息,即客戶名稱,客戶地址,客戶電子郵件和客戶聯系電話。

我想知道如何使用Java從文本字段獲取數據並將其放入netbeans的Derby數據庫中。

好吧,您需要首先從字段中獲取文本,如下所示:

//Replace the textfield names with your textfield variable names
String customerName = txtFieldCustomerName.getText();
String customerAddress = txtFieldCustomerAddress.getText();
String customerEmail = txtFieldCustomerEmail.getText();
String customerContactNumber = txtFieldCustomerContactNumber.getText();

現在我們有了所有數據,我們可以執行數據庫插入

Connection con = null;
PreparedStatement pstmt = null;
try {
   Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
        //Get a connection
   con = DriverManager.getConnection("jdbc:derby://localhost:1527/myDB;create=true;user=me;password=mine");//Replace this with your information to your database

   //now we have a connection, we can perform the insert
   pstmt = con.prepareStatement("insert into TABLE_NAME_HERE (customerName, customerAddress, customerEmail, customerContactNumber) VALUES (?, ?, ?, ?)");
   pstmt.prepareString(1, customerName);
   pstmt.prepareString(2, customerAddress);
   pstmt.prepareString(3, customerEmail);
   pstmt.prepareString(4, customerContactNumber);

   pstmt.executeUpdate(); //execute the insert
} catch(SQLException sqle) {
   sqle.printStackTrace();
}
finally { //close the connection after everything is done.
   try {
      con.close();
      pstmt.close();
   } catch(SQLException sqle) {
       sqle.printStackTrace();
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM