java连接mySQL数据库的问题,连接数据库什么的都没问题,但插入数据的操作运行不了!!不知道什么原因,求大神赐教!!!感激涕零!!

宇奈 2014-06-15 02:25:30

package com;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.sql.*;

public class all extends JFrame implements ActionListener
{
JTextArea result_tf;
JLabel label_1,label_2;
JTextField number,id;
JButton calculate,clear,delete,insert,yes;
JPanel p;
Connection conn;
Statement stmt;
PreparedStatement stmt1;
ResultSet rs;
dialog dlg;

public class dialog extends JDialog implements ActionListener
{
dialog(JFrame f,boolean b)
{
super(f,b);
setLayout(new FlowLayout());
label_2=new JLabel("请输入ID");
id=new JTextField(9);
yes=new JButton("确定");
add(label_2);
add(id);
add(yes);
validate();
yes.addActionListener(this);
setBounds(60,60,360,100);

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
setVisible(false);
}
});

}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==yes)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:MyDatabase","","");
conn.setAutoCommit(false);
stmt1=conn.prepareStatement("insert into StuInfo(学号) values(?)");
//stmt1.setInt(1,Integer.parseInt(id.getText()));

stmt1.setString(1,id.getText());
stmt1.executeUpdate("insert into StuInfo(学号) values(?)");
conn.commit();
JOptionPane.showMessageDialog(this,new String("插入成功"));
stmt1.close();
conn.close();
}
catch(Exception a)
{
a.printStackTrace();
try
{
JOptionPane.showMessageDialog(this,new String("插入失败"));
conn.rollback();

}
catch(Exception ax)
{
ax.printStackTrace();
}
}
}
}



}

public all()
{
super("查询数据库");
p=new JPanel();
label_1=new JLabel("请输入要删除的学号");

number=new JTextField(9);

//desc=new JTextField(9);
calculate=new JButton("查询所有行");
clear=new JButton("清除");
delete=new JButton("删除操作");
insert=new JButton("插入数据");
result_tf=new JTextArea(30,30);
p.setLayout(new FlowLayout());
p.add(label_1);
p.add(number);
p.add(delete);
p.add(calculate);
p.add(clear);


//p.add(desc);
p.add(insert);
p.add(result_tf);
add(p);
dlg=new dialog(this,true);

calculate.addActionListener(this);
clear.addActionListener(this);
delete.addActionListener(this);
insert.addActionListener(this);
setSize(350,200);
setLocation(200,200);
dlg.setResizable(false);
setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setDefaultLookAndFeelDecorated(true);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==calculate)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException a)
{
System.out.println("加载驱动程序错误!"+a);
}
try
{
conn=DriverManager.getConnection("jdbc:odbc:MyDatabase","","");
stmt=conn.createStatement();
rs=stmt.executeQuery("SELECT * FROM StuInfo");
String rcontent = null;
while(rs.next())
{
rcontent+=(rs.getString(1)+" "+rs.getString(2)+" "
+rs.getString(3)+" "+rs.getString(4)+" "+rs.getString(5)
+" "+rs.getString(6)+" "+"\n");
}
result_tf.setText(rcontent);
rs.close();
stmt.close();
conn.close();
}
catch(SQLException a)
{
System.out.println("访问数据库错误!"+a);
}
}
else if(e.getSource()==delete)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:MyDatabase","","");
conn.setAutoCommit(false);
stmt1=conn.prepareStatement("delete from StuInfo where 学号=?");


stmt1.setInt(1,Integer.parseInt(number.getText()));
stmt1.executeUpdate(/*"delete from jobs where job_id=?"*/);
conn.commit();
JOptionPane.showMessageDialog(this,new String("删除成功"));
stmt1.close();
conn.close();
}
catch(Exception a)
{
a.printStackTrace();
try
{
JOptionPane.showMessageDialog(this,new String("删除失败"));
conn.rollback();

}
catch(Exception ax)
{
ax.printStackTrace();
}
}
}
else if(e.getSource()==insert)
{
dlg.setVisible(true);


}
else
{
result_tf.setText("");
}
}

public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable()
{

@Override
public void run() {
// TODO Auto-generated method stub
new all();
}

});

}
}

...全文
1688 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dgqjava 2014-06-20
  • 打赏
  • 举报
回复
代码问题太多了, 不过把 stmt1.executeUpdate("insert into StuInfo(学号) values(?)"); 改成 stmt1.executeUpdate(); 应该就可以插入数据
tony4geek 2014-06-20
  • 打赏
  • 举报
回复
数据库里可以不
逍遥jc 2014-06-20
  • 打赏
  • 举报
回复
首先把sql语句直接复制到数据库运行吧。
  • 打赏
  • 举报
回复
stmt1.executeUpdate("insert into StuInfo(学号) values(?)"); 你这些参数是做什么用的?
-江沐风- 2014-06-15
  • 打赏
  • 举报
回复
单独将处理mysql的那段SQL拿出来试试;
宇奈 2014-06-15
  • 打赏
  • 举报
回复
在对话框dialog那里啊
之奇一昂 2014-06-15
  • 打赏
  • 举报
回复
else if(e.getSource()==insert) {//这里面并没有插入数据库操作啊…… dlg.setVisible(true); }

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧