我制作了一个GUI。当用户点击JButton b1
时,可以插入数据。后端数据库有1个表,它是电影,有两列。第一列的名称是title
,数据类型是varchar
,第二列的名称是year
,它有数据类型integer
。但我不知道如何在程序中借助文本字段编写此查询。我在查询时尝试了很多代码,但没有任何事情发生
代码:
public class A extends JFrame{
private JTextField t1;
private JTextField t2;
private JLabel l1;
private JLabel l2;
private JButton b1;
private Connection conn;
private Statement state;
String server = "jdbc:mysql://localhost/demo";
String user="root";
String pass="65";
public A(){
super("Query");
setLayout(null);
t1= new JTextField();
t1.setBounds(173, 152, 128, 27);
add(t1);
//for year
t2= new JTextField();
t2.setBounds(173, 105, 128, 27);
add(t2);
l1= new JLabel("title");
l1.setBounds(99, 101, 42, 37);
add(l1);
l1= new JLabel("year");
l1.setBounds(99, 148, 42, 37);
add(l1);
b1= new JButton("Insert");
b1.setBounds(50, 222, 102, 37);
add(b1);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
CreateQuery("insert into movies (title,year) values('"+t1.getText()+"') ");
}
catch(Exception e1){
e1.printStackTrace();
}
}
});
setSize(450,450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public void CreateQuery(String w){
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(server,user,pass);
state = conn.createStatement();
state.executeUpdate(w);
JOptionPane.showMessageDialog(null, "Query Executed");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
}
主:
public class Main {
public static void main(String[] args) {
A obj = new A();
}
}
答案 0 :(得分:1)
您insert into movies (title,year) values('"+t1.getText()+"')
需要两个参数,而您只发送一个参数。
--- 修改强> ---
将Statement
替换为PreparedStatement
,并使用setInt
和setString
方法的参数。这是一个很好的做法,请查看以下链接http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
答案 1 :(得分:1)
您可以通过两种方式执行此操作:第一种是您正在使用的过程,即使用createStatement
在您的情况下,您必须设置标题和年份。
CreateQuery("insert into movies (title,year) values('"+t1.getText()+"', " + t2.getText() + ")");
第二个是使用preparedStatement, 方法调用,
CreateQuery(t1.getText(), t2.getText());
的方法,
public void CreateQuery(String title, String year){
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(server,user,pass);
PreparedStatement state = conn.prepareStatement("insert into movies (title,year) values('?', ?)");
state.setString(1, title);
state.setInt(1, new Integer(year));
state.executeUpdate();
JOptionPane.showMessageDialog(null, "Query Executed");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
答案 2 :(得分:0)
通过发送两个参数,即标题&一年可以解决问题
修改后的代码
CreateQuery("insert into movies (title,year) values('"+t1.getText()+"',"+t2.getText()+") ");
如果您有年度字段的任何默认值,则下面是修改后的代码。
CreateQuery("insert into movies (title) values('"+t1.getText()+"') ");
答案 3 :(得分:0)
b1= new JButton("Insert");
b1.setBounds(50, 222, 102, 37);
add(b1);
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
int w = Integer.parseInt(t2.getText());
CreateQuery("insert into movies (title,year) values('"+t1.getText()+"',"+w+") ");
}
catch(Exception e1){
e1.printStackTrace();
}
点击
java.lang.NumberFormatException: For input string: "World"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Queryahmad.C_03Update$1.actionPerformed(C_03Update.java:76)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)