简体   繁体   English

在mysql表中插入数据时出错

[英]error while inserting data inside mysql table

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.我想将用户在 JFrame 中输入的数据插入到我的 sql 数据库中。 tf1 and tf2 are textfields for username and email_id, respectively. tf1tf2分别是 username 和 email_id 的文本字段。 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. id是我数据库中的一列,表示 1、2、3 等数字。 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.尽管您尚未共享表的结构,但我猜想idPRIMARY KEY但您错过了AUTO_INCREMENT配置。 In your database, run the following statement在您的数据库中,运行以下语句

ALTER TABLE user_info MODIFY COLUMN id INT auto_increment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM