1个回答
2013-12-06
展开全部
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;class StudentApplication implements ActionListener
{
JFrame fMain;
JPanel pan;
JLabel labId,labName,labSex,labAge;
JTextField texId,texName,texSex,texAge;
JButton butQuery,butAdd,butUpdate,butDelete;
Connection con;
PreparedStatement ps;
ResultSet rs;
public StudentApplication()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Hospital","sa","123456");
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Connection con=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=Hospital_Table;user=sa;password=123456");
}catch(Exception er)
{
JOptionPane.showMessageDialog(fMain,er.getMessage());
}
formStudent();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==butQuery)
{
try{
ps=con.prepareStatement("select * from Hospital_Employee where Employee_ID=?");
ps.setString(1,texId.getText());
ResultSet rs= ps.executeQuery();
if(rs.next())
{
texName.setText(rs.getString(2));
texSex.setText(rs.getString(3));
texAge.setText(Integer.toString(rs.getInt(4)));
}
else
{
JOptionPane.showMessageDialog(fMain,"查询失败!");
}
}catch(Exception e1)
{
JOptionPane.showMessageDialog(fMain,e1.getMessage());
}
}
if(e.getSource()==butAdd)
{
try{
ps=con.prepareStatement("insert students values(?,?,?,?)");
ps.setString(1,texId.getText());
ps.setString(2,texName.getText());
ps.setString(3,texSex.getText());
ps.setInt(4,Integer.parseInt(texAge.getText()));
int Ruselt=ps.executeUpdate();
if(Ruselt>0)
{
JOptionPane.showMessageDialog(fMain,"增加成功");
}
else
{
JOptionPane.showMessageDialog(fMain,"增加失败!");
}
}catch(Exception e2)
{
JOptionPane.showMessageDialog(fMain,e2.getMessage());
}
}
if(e.getSource()==butUpdate)
{
try{
ps=con.prepareStatement("update students set stu_name=?,sex=?,age=? where stu_id=?");
ps.setString(1,texName.getText());
ps.setString(2,texSex.getText());
ps.setInt(3,Integer.parseInt(texAge.getText()));
ps.setString(4,texId.getText());
int Ruselt=ps.executeUpdate();
if(Ruselt>0)
{
JOptionPane.showMessageDialog(fMain,"修改成功");
}
else
{
JOptionPane.showMessageDialog(fMain,"修改失败");
}
}catch(Exception e2)
{
JOptionPane.showMessageDialog(fMain,e2.getMessage());
}
}
if(e.getSource()==butDelete)
{
try{
ps=con.prepareStatement("delete students where stu_id=?");
ps.setString(1,texId.getText());
int Ruselt=ps.executeUpdate();
if(Ruselt>0)
{
JOptionPane.showMessageDialog(fMain,"删除成功");
}
else
{
JOptionPane.showMessageDialog(fMain,"删除失败");
}
}catch(Exception e2)
{
JOptionPane.showMessageDialog(fMain,e2.getMessage());
}
}
}
private void formStudent()
{
fMain=new JFrame("学生信息");
pan=new JPanel();
labId=new JLabel("学号");
labName=new JLabel("名字");
labSex=new JLabel("性别");
labAge=new JLabel("年龄");
texId=new JTextField(15);
texName=new JTextField(15);
texSex=new JTextField(6);
texAge=new JTextField(6);
butQuery=new JButton("查询");
butAdd=new JButton("增加");
butUpdate=new JButton("修改");
butDelete=new JButton("删除");
fMain.add(pan);
pan.setLayout(null);
labId.setBounds(10,10,60,25);
labName.setBounds(10,55,60,25);
labSex.setBounds(10,100,60,25);
labAge.setBounds(10,145,60,25);
texId.setBounds(80,10,100,25);
texName.setBounds(80,55,100,25);
texSex.setBounds(80,100,100,25);
texAge.setBounds(80,145,100,25);
butQuery.setBounds(200,10,80,25);
butAdd.setBounds(200,50,80,25);
butUpdate.setBounds(200,100,80,25);
butDelete.setBounds(200,145,80,25);
pan.add(labId);
pan.add(labName);
pan.add(labSex);
pan.add(labAge);
pan.add(texId);
pan.add(texName);
pan.add(texSex);
pan.add(texAge);
pan.add(butQuery);
pan.add(butAdd);
pan.add(butUpdate);
pan.add(butDelete);
fMain.setSize(320,222);
fMain.setVisible(true);
butQuery.addActionListener(this);
butAdd.addActionListener(this);
butUpdate.addActionListener(this);
butDelete.addActionListener(this);
}
public static void main(String[] args)
{
StudentApplication sa=new StudentApplication();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;class StudentApplication implements ActionListener
{
JFrame fMain;
JPanel pan;
JLabel labId,labName,labSex,labAge;
JTextField texId,texName,texSex,texAge;
JButton butQuery,butAdd,butUpdate,butDelete;
Connection con;
PreparedStatement ps;
ResultSet rs;
public StudentApplication()
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:Hospital","sa","123456");
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
// Connection con=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=Hospital_Table;user=sa;password=123456");
}catch(Exception er)
{
JOptionPane.showMessageDialog(fMain,er.getMessage());
}
formStudent();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==butQuery)
{
try{
ps=con.prepareStatement("select * from Hospital_Employee where Employee_ID=?");
ps.setString(1,texId.getText());
ResultSet rs= ps.executeQuery();
if(rs.next())
{
texName.setText(rs.getString(2));
texSex.setText(rs.getString(3));
texAge.setText(Integer.toString(rs.getInt(4)));
}
else
{
JOptionPane.showMessageDialog(fMain,"查询失败!");
}
}catch(Exception e1)
{
JOptionPane.showMessageDialog(fMain,e1.getMessage());
}
}
if(e.getSource()==butAdd)
{
try{
ps=con.prepareStatement("insert students values(?,?,?,?)");
ps.setString(1,texId.getText());
ps.setString(2,texName.getText());
ps.setString(3,texSex.getText());
ps.setInt(4,Integer.parseInt(texAge.getText()));
int Ruselt=ps.executeUpdate();
if(Ruselt>0)
{
JOptionPane.showMessageDialog(fMain,"增加成功");
}
else
{
JOptionPane.showMessageDialog(fMain,"增加失败!");
}
}catch(Exception e2)
{
JOptionPane.showMessageDialog(fMain,e2.getMessage());
}
}
if(e.getSource()==butUpdate)
{
try{
ps=con.prepareStatement("update students set stu_name=?,sex=?,age=? where stu_id=?");
ps.setString(1,texName.getText());
ps.setString(2,texSex.getText());
ps.setInt(3,Integer.parseInt(texAge.getText()));
ps.setString(4,texId.getText());
int Ruselt=ps.executeUpdate();
if(Ruselt>0)
{
JOptionPane.showMessageDialog(fMain,"修改成功");
}
else
{
JOptionPane.showMessageDialog(fMain,"修改失败");
}
}catch(Exception e2)
{
JOptionPane.showMessageDialog(fMain,e2.getMessage());
}
}
if(e.getSource()==butDelete)
{
try{
ps=con.prepareStatement("delete students where stu_id=?");
ps.setString(1,texId.getText());
int Ruselt=ps.executeUpdate();
if(Ruselt>0)
{
JOptionPane.showMessageDialog(fMain,"删除成功");
}
else
{
JOptionPane.showMessageDialog(fMain,"删除失败");
}
}catch(Exception e2)
{
JOptionPane.showMessageDialog(fMain,e2.getMessage());
}
}
}
private void formStudent()
{
fMain=new JFrame("学生信息");
pan=new JPanel();
labId=new JLabel("学号");
labName=new JLabel("名字");
labSex=new JLabel("性别");
labAge=new JLabel("年龄");
texId=new JTextField(15);
texName=new JTextField(15);
texSex=new JTextField(6);
texAge=new JTextField(6);
butQuery=new JButton("查询");
butAdd=new JButton("增加");
butUpdate=new JButton("修改");
butDelete=new JButton("删除");
fMain.add(pan);
pan.setLayout(null);
labId.setBounds(10,10,60,25);
labName.setBounds(10,55,60,25);
labSex.setBounds(10,100,60,25);
labAge.setBounds(10,145,60,25);
texId.setBounds(80,10,100,25);
texName.setBounds(80,55,100,25);
texSex.setBounds(80,100,100,25);
texAge.setBounds(80,145,100,25);
butQuery.setBounds(200,10,80,25);
butAdd.setBounds(200,50,80,25);
butUpdate.setBounds(200,100,80,25);
butDelete.setBounds(200,145,80,25);
pan.add(labId);
pan.add(labName);
pan.add(labSex);
pan.add(labAge);
pan.add(texId);
pan.add(texName);
pan.add(texSex);
pan.add(texAge);
pan.add(butQuery);
pan.add(butAdd);
pan.add(butUpdate);
pan.add(butDelete);
fMain.setSize(320,222);
fMain.setVisible(true);
butQuery.addActionListener(this);
butAdd.addActionListener(this);
butUpdate.addActionListener(this);
butDelete.addActionListener(this);
}
public static void main(String[] args)
{
StudentApplication sa=new StudentApplication();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询