我不明白为什么我会出错,我相信我做的一切都是正确的。当我尝试将文本从 combobx 中取出时,我似乎只会遇到错误。有人可以帮我做好这件事吗?我知道如何将文本字段中的数据插入到具有 microsoft access 的数据中。但在组合框方面则不然。
论文错误
java.lang.NullPointerException at delete_me1.connection(delete_me1.java:66) at delete_me1$2.actionPerformed(delete_me1.java:133) at java.awt.EventDispatchThread.run(Unknown Source) java.lang.NullPointerException at delete_me1.connection( delete_me1.java:90) 在 delete_me1$2.actionPerformed(delete_me1.java:133)
谢谢 :)
import java.awt.EventQueue;
public class delete_me1 {
private JFrame frame;
public JComboBox<?> comboBox;
String MSAccessDriver = "sun.jdbc.odbc.JdbcOdbc ".trim();
String MyDatabase = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\db\\Database.mdb";
Connection con = null;
Statement st = null;
ResultSet rs;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
delete_me1 window = new delete_me1();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void connection() throws Exception {
/* String sql = "INSERT INTO animal (first_name, last_name) VALUES(?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,animal);
preparedStatemnt.setString(2,animalType);
preparedStatemnt.executeUpdate();*/
try {
String com2 = comboBox.getSelectedItem().toString();
String com = textField.getText();
String sql = "INSERT INTO client (first_name, last_name) VALUES (?,?)";
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1,com);
preparedStatement.setString(2,com2);
preparedStatement.executeUpdate();
Class.forName(MSAccessDriver);
String db = "jdbc:odbc:Food"; // db = database string stored
// in the database
con = DriverManager.getConnection(db);
st = con.createStatement();
st.execute(sql);
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JOptionPane.showMessageDialog(null, "Save Complete Successfully");
}
}
/**
* Create the application.
*/
public delete_me1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
final JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] { "Dog", "Cat",
"Cow", "Sheep" }));
comboBox.setBounds(57, 54, 245, 46);
frame.getContentPane().add(comboBox);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
connection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnNewButton.setBounds(57, 143, 89, 23);
frame.getContentPane().add(btnNewButton);
textField = new JTextField();
textField.setBounds(172, 144, 118, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
}
}