如何将文本框中的数据插入到 java-netbeans 中的 mysql 数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18488519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to insert data from textboxes into mysql database in java-netbeans
提问by Dinesh Raja
I am new to programming and i am trying to make an small Java swing application using netbeans IDE and i have designed the Form and created an table too i used the following code to insert data into database from the form but i am getting many errors please help me to correct this code:
我是编程新手,我正在尝试使用 netbeans IDE 制作一个小型 Java Swing 应用程序,我设计了表单并创建了一个表,我使用以下代码将数据从表单插入数据库,但我遇到了很多错误帮我更正这段代码:
import java.sql.*;
public class db
{
static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/userdb";
static final String USER="root";
static final String PASS="toor";
Connection conn = null;
Statement stmt = null;
static final String d_unit=jTextField2.getText();
static final String d_name=jTextField3.getText();
static final String d_dob=jDateChooser2.getText();
//static final String d_gender="gender";
static final String d_age=jTextField4.getText();
static final String d_doorno=jTextField5.getText();
static final String d_street=jTextField6.getText();
static final String d_vc=jTextField7.getText();
static final String d_district=jTextField8.getText();
static final String d_pin=jTextField9.getText();
static final String d_phone=jTextField10.getText();
static final String d_mail=jTextField11.getText();
static final String d_occupations=jTextField12.getText();
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
stmt.executeUpdate("insert into donors (unit,name,dob,age,doorno,street,vc,district,pin,phone,mail,occupation) values('"+d_unit+"','"+d_name+"','"+d_dob+"','"+d_age+"','"+d_doorno+"','"+d_street+"','"+d_vc+"','"+d_district+"','"+d_pin+"','"+d_phone+"','"+d_mail+"','"+d_occupations+"')");
JOptionPane.showMessageDialog(null,"Inserted Successfully!");
}
catch(Exception e)
{ }
}
回答by WearFox
You may not use the final String
because, then you can't modify these Strings, and the other code is correct, but i think you can use the ?
in the line:
你可能不使用final String
因为,那么你不能修改这些字符串,其他代码是正确的,但我认为你可以?
在行中使用:
String sql="INSERT INTO ′donors′ (unit,name) VALUES (?,?)";
//put the rest of the sentence
try {
PreparedStatement pdt = cn.prepareStatement(sql);
pdt.setString(1, jTextField2.getText();
pdt.setString(2, jTextField3.getText();
//put the rest of the code
int n1=pdt.executeUpdate();
if(n1>0)
{
JOptionPane.showMessageDialog(null,"Inserted Successfully!");
}
}catch (SQLException ex) { }
Well, that's the largest way, but the most correct. I hope this helps.
嗯,这是最大的方式,但最正确的。我希望这有帮助。
回答by KNU
try proceeding like this for simplicity and less error-prone
为简单起见且不易出错,请尝试这样做
String sql = "INSERT INTO donors(unit,name,dob,age,doorno,street,vc,district,pin,phone,mail,occupation) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)";
stmt.executeUpdate(sql);
回答by malinda
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {
String itemCode = txtItemCode.getText();
String itemName = txtItemName.getText();
String unitPrice = txtUnitPrice.getText();
String qty = txtQty.getText();
String query = "insert into items values ('"+itemCode+"','"+itemName+"','"+unitPrice+"','"+qty+"')";
System.out.println(query);
try {
Connection c = DBClass.getConnection();
Statement stmt = c.createStatement();
stmt.executeUpdate(query);
JOptionPane.showMessageDialog(this, "Saved");
} catch (Exception e) {
e.printStackTrace();
}
// DBClass
// 数据库类
import java.sql.Connection;
import java.sql.DriverManager;
/**
*
* @author Nadun
*/
public class DBClass {
static private Connection connection;
public static Connection getConnection() throws Exception{
if(connection == null){
//JDBC
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/stock", "root", "123");
}
return connection;
}
}
回答by Dio Lantief Widoyoko
Everything looks alright. Maybe the trouble is on your mysql database?
一切看起来都很好。也许问题出在你的 mysql 数据库上?
Check the data type of your row in mysql, if your data type in the current row is "int", then it should be like this
在mysql中检查你所在行的数据类型,如果你当前行的数据类型是“int”,那么应该是这样的
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.createStatement();
stmt.executeUpdate("insert into donors (name, age) values('"+d_name+"',,'"+Integer.valueOf(d_age.getText().toString())+"')");
JOptionPane.showMessageDialog(null,"Inserted Successfully!");
}
catch(Exception e){ }
}
You should be careful with data types. If your mysql row is int type then in your java you should give it int time as well.
您应该小心使用数据类型。如果你的 mysql 行是 int 类型,那么在你的 java 中你也应该给它 int 时间。
I guess your data type of "name" row is text that use string, and your data type of "age" row is int?
我猜你的“name”行的数据类型是使用字符串的文本,而你的“age”行的数据类型是int?
I check your code is giving a string value from your java to your int mysql row. that's the error.
我检查您的代码是否将字符串值从您的 java 提供给您的 int mysql 行。这就是错误。
So you should convert the string to the int first
所以你应该先将字符串转换为 int
Integer.valueOf(d_age.getText().toString());