日期:2014-05-20 浏览次数:20748 次
//对数据库的操作进行一个封装
//SqlOper.java
package homework;
import java.sql.*;
public class SqlOper {
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
String url="jdbc:microsoft:sqlserver://127.0.0.1:1434;databaseName=homework";
String user="sa";
String passwd="sa";
String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
//关闭数据库资源
public void close()
{
try {
if(rs!=null) rs.close();
if(ps!=null) ps.close();
if(ct!=null) ct.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
//把增删改合并在一起,参数分别是:sql语句和注入值的数组
public boolean updataExe(String sql,String []data)
{
boolean b=true;
try {
Class.forName(driver);
ct=DriverManager.getConnection(url,user,passwd);
ps=ct.prepareStatement(sql);
///给ps注入值
for(int i=0;i<data.length;i++)
{
ps.setString(i+1,data[i]);
}
if(ps.executeUpdate()==0)
b=false;
} catch (Exception e) {
b=false;
e.printStackTrace();
}finally{
this.close();
}
return b;
}
//查询数据库操作
public ResultSet queryExe(String sql,String []data)
{
try {
Class.forName(driver);
ct=DriverManager.getConnection(url,user,passwd);
ps=ct.prepareStatement(sql);
///给ps注入值
for(int i=0;i<data.length;i++)
{
ps.setString(i+1,data[i]);
}
rs=ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}finally{
this.close();
}
return rs;
}
}
//EmpManage.java
package homework;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class EmpManage extends JFrame implements ActionListener{
static JTable jt;
JScrollPane jsp;
JLabel jl;
JButton jb1,jb2,jb3,jb4,jb5;
JTextField jtf;
JPanel jp1,jp2;
static EmpModel em;
//当做修改操作时,选择一行
static int rowNo;
public static void main(String[] args) {
EmpManage e=new EmpManage();
}
public EmpManage()
{
jt=new JTable(em);
F5();
jsp=new JScrollPane(jt);
jl=new JLabel("员工姓名");
jtf=new JTextField(10);
jb1=new JButton("查询");
jb1.addActionListener(this);
jb2=new JButton("刷新");
jb2.addActionListener(this);
jb3=new JButton("修改");
jb3.addActionListener(this);
jb4=new JButton("添加");
jb4.addActionListener(this);
jb5=new JButton("删除");
jb5.addActionListener(this);
jp1=new JPanel();
jp1.add(jl);jp1.add(jtf);jp1.add(jb1);
this.add(jp1,"North");
jp2=new JPanel();
jp2.add(jb2);jp2.add(jb3);jp2.add(jb4);jp2.add(jb5);
this.add(jp2,"South");
this.add(jsp);
this.setSize(650, 650);
this.setVisible(true);
}
//刷新操作
static public void F5()
{
String sql="select * from emp where 1=?";
String []data={"1"};
em=new EmpModel();
em.queryEmp(sql,data);
jt.setModel(em);
}
@Override
public void actionPerformed(ActionEvent arg0) {
//查询
if(arg0.getSource()==jb1)
{
String empname=jtf.getText().trim();
String sql="select * from emp where empname=?";
String []data={empname};
em=new EmpModel();
em.queryEmp(sql, data);
jt.setModel(em);
}
//刷新
else if(arg0.getSource()==jb2)
{
F5();
}
//这两个操作跟前面的类似,就不用管了》。。
//修改
// else if(arg0.getSource()==jb3)
// {
// rowNo=this.jt.getSelectedRow();
// if(rowNo==-1)
// {
// JOptionPane.showMessageDialog(this, "请选择一行");
// return ;
// }
// (new UpdataEmp()).getContentPane();
// }
//添加
// else if(arg0.getSource()==jb4)
// {
// (new AddEmp()).getContentPane();
// }
//删除
else if(arg0.getSource()==jb5)
{
rowNo=this.jt.getSelectedRow();
if(rowNo==-1)
{
JOptionPane.showMessageDialog(this, "请选择一行");
return ;
}
String id=(String)em.getValueAt(rowNo, 0);
String sql="delete from emp where id=?";
String []data={id};
em.updataEmp(sql, data);
F5();
}
}
}
EmpModel.java
package homework;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;