我尝试过类似的东西:但错误出现了:
严重:空 空值 com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:列'MYUSER'不能为空
虽然不应该?另外我想问一下我的程序中是否存在一些不良做法。 任何提示?
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
public class Main extends JFrame {
String text;
public Main() throws HeadlessException {
//opis
JLabel opis = new JLabel("Opis: ");
JTextField opisTextField = new JTextField();
opisTextField.setPreferredSize(new Dimension(100, 20));
add(opis);
add(opisTextField);
//jak klikniemy w przycisk program doda wartosci wpisane do bazy danych.
Button z = new Button("Dodaj do bazy danych");
add(z);
z.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e){}
@Override
public void mouseEntered(MouseEvent e){}
@Override
public void mouseExited(MouseEvent e){}
@Override
public void mouseReleased(MouseEvent e){}
@Override
public void mousePressed(MouseEvent e) {
Main m = new Main();
m.text = opisTextField.getText();
});
}
public void readDataBase() throws Exception {
try {
// this will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
connect = DriverManager
.getConnection("jdbc:mysql://localhost/feedback?"
+ "user=sqluser&password=sqluserpw");
// statements allow to issue SQL queries to the database
statement = connect.createStatement();
// resultSet gets the result of the SQL query
// preparedStatements can use variables and are more efficient
preparedStatement = connect
.prepareStatement("insert into FEEDBACK.COMMENTS values (default, ?, ?, ?, ? , ?, ?)");
// "myuser, webpage, datum, summary, COMMENTS from FEEDBACK.COMMENTS");
// parameters start with 1
preparedStatement.setString(1, text);
preparedStatement.executeUpdate();
// remove again the insert comment
} catch (ClassNotFoundException | SQLException e) {
throw e;
} finally {
// close();
}
}
public static void main(String[] args) throws Exception {
new Main().setVisible(true);
}
}
答案 0 :(得分:0)
很抱歉无法浏览所有代码,但看起来您已声明String text;
并且您从未向其提供过值,并且您将此值传递给用户名preparedStatement.setString(1, text);
,根据错误,该值不能为null。