String pass=new String(pf1.getPassword());
try {
Connection myConn=DriverManager.getConnection("jdbc:mysql://localhost:3306/javaproject","root","noor1032");
PreparedStatement myStat=myConn.prepareStatement("insert into user_info (username,password,email_id)"+"values(?,?,?)");
myStat.setString(1, tf1.getText());
myStat.setString(2, pass);
myStat.setString(3, tf2.getText());
myStat.execute();
myConn.close();
}
catch(Exception f)
{
System.err.println("Got an exception!");
System.err.println(f.getMessage());
}
I want to insert data entered by user in a JFrame to my sql database. tf1
and tf2
are textfields for username and email_id, respectively. When I execute this statement an exception occurs saying that
Field 'id' does not have a default value
id
is a column in my database denoting numbers like 1,2,3 and so on. Please help me.
您必须在 mysql 中将 ID 设置为 Auto Increment。它会起作用
Although you haven't shared your table's structure, I guess that id
is a PRIMARY KEY
but you missed AUTO_INCREMENT
configuration. In your database, run the following statement
ALTER TABLE user_info MODIFY COLUMN id INT auto_increment
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.