테스트 목적으로 2 개의 jTextFields, 버튼 및 테이블이있는 양식을 만들었습니다. 데이터베이스에 데이터를 추가하려고합니다. 하지만 문제가있는 것 같습니다. 버튼을 누르면 데이터가 데이터베이스에 추가되지 않았기 때문에 실패 메시지가 반환됩니다. 도움을 주시면 감사하겠습니다. 그래서 여기에 내가하는 일이 있습니다. 코드를 단순화하기 위해 ConnectionConfiguration 클래스를 만들었습니다.
public class ConnectionConfiguration {
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connection Success");
} catch(ClassNotFoundException e) {
System.out.println("Connection Failed");
}
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/systemnew?zeroDateTimeBehavior=convertToNull", "root","123456");
System.out.println("Database Connected");
} catch (SQLException se){
System.out.println("No Database" + se);
}
return connection;
}
}
연결은 항상 성공하고 데이터베이스는 항상 연결됩니다. 오류 메시지는 내 실수가 여기에 있음을 나타냅니다 (systemnew.UpdateDatabase.add에 있음). 다음은 UpdateDatabase 클래스의 add 메서드입니다.
public boolean add(String field1, String field2) {
try {
Connection conn = ConnectionConfiguration.getConnection();
PreparedStatement ps = conn.prepareStatement("INSERT INTO newtable(field1,field2) VALUES('"+field1+"','"+field2+"')");
ps.executeUpdate();
return true;
} catch(Exception ex){
ex.printStackTrace();
}
return false;
}
잠재적으로 데이터베이스에 데이터를 추가해야하는 버튼의 코드는 다음과 같습니다.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(new UpdateDatabase().add(jTextField1.getText(),jTextField2.getText())){
JOptionPane.showMessageDialog(null, "Added successfully!");
} else {
JOptionPane.showMessageDialog(null, "Record has not been added!");
}
}
오류
Connection Success
Database Connected
java.sql.SQLException: Field 'tblid' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:963)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
at systemnew.UpdateDatabase.add(UpdateDatabase.java:38)
오류는 특정 문제를 알려줍니다.
java.sql.SQLException: Field 'tblid' doesn't have a default value
테이블 에 삽입 중에 값을 지정하지 않은 newtable
널이 아닌 tblid
열이있는 것 같습니다. 다음과 같이 표시 할 수 있습니다.auto increment
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다