我想将从文本框中输入的文字添加到数据库当中,控制台显示的是“数据添加成功的提示”,但是数据库那边id号虽然自增长了,可Name列中却是空的?为什么?
(想贴图但是我新手不知道怎么贴,只能委屈大神看看这没有高亮的代码了...)
JPanel GongXiPanel = new JPanel(); // 新建面板保存标签
GongXiPanel.setLayout(new GridLayout(3, 1, 5, 5)); // 将面板设置成流式布局
getContentPane().add(GongXiPanel, BorderLayout.NORTH);
JLabel Congra = new JLabel("恭喜你!",SwingConstants.CENTER);
Congra.setFont(new Font("宋体",Font.BOLD,30));
GongXiPanel.add(Congra);
winName = new JTextField();
NewName = winName.getText().toString();
winName.setVisible(true);
winName.setBounds(new Rectangle(100, 140, 143, 22));
GongXiPanel.add(winName);
I_am = new JButton("确定");
I_am.setVisible(true);
I_am.setBounds(new Rectangle(20, 20, 20, 20));
GongXiPanel.add(I_am);
winName.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
winName.requestFocus();
}
});
I_am.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
SaveWinner save = new SaveWinner();
save.setRecord(NewName);
}
});
public class SaveWinner{
private String name = null;
public void setRecord(String newName) {
name = newName;
Connection conn = null;
conn = DBConnection.getConnection();
try {
PreparedStatement statement = conn.prepareStatement("insert into Winner(winnerName) values(?)");
statement.setString(1, name);
statement.executeUpdate();
statement.close();
conn.close();
System.out.println("插入成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
补充一下,我是想将用户输入的名字添加到数据库中,表里只有id列和Name列,id设为自增长。
现在虽然id会自动增长,但是Name列中的没有文字的,是空的,我不懂怎么回事。。。(我是新手)