需要连接数据库,然后有一些问题....
我eclipse和sql server2005连接正常,驱动,该升级的也有,我写了一个小方法做了测试,对数据库操作是可以成功的。我贴出三个java文件。最终效果大概如下图。代码可能有些多,请热心的朋友帮我看看,我为此困扰了一下午,非常无语中。。。
//对数据库的操作进行一个封装
//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;
import javax.swing.table.*;
public class EmpModel extends AbstractTableModel{
Vector rowData,columnNames;
public boolean updataEmp(String sql,String []data)
{
SqlOper so=new SqlOper();
return so.updataExe(sql, data);
}
public void queryEmp(String sql,String []data)
{
SqlOper so = null;
columnNames=new Vector();
columnNames.add("员工编号");
columnNames.add("姓名");
columnNames.add("性别");
columnNames.add("家庭住址");
columnNames.add("出生日期");
columnNames.add("职位");
columnNames.add("电话号码");
columnNames.add("注册时间");
columnNames.add("备注");
rowData=new Vector();
try {
so=new SqlOper();
ResultSet rs=so.queryExe(sql, data);
while(rs.next())
{
Vector hang=new Vector();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getString(4));
hang.add(rs.getString(5));
hang.add(rs.getString(6));
hang.add(rs.getString(7));
hang.add(rs.getString(8));
hang.add(rs.getString(9));
rowData.add(hang);
}
} catch (Exception e) {
e.printStackTrace();
}finally
{
so.close();
}
}
//多少列
public int getColumnCount() {
return this.columnNames.size();
}
//多少行
public int getRowCount() {
return this.rowData.size();
}
@Override
public String getColumnName(int arg0) {
return (String)this.columnNames.get(arg0);
}
//某行某列数据
public Object getValueAt(int arg0, int arg1) {
return ((Vector) this.rowData.get(arg0)).get(arg1);
}
}
//对数据库的操作进行一个封装
//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;
import javax.swing.table.*;
public class EmpModel extends AbstractTableModel{
Vector rowData,columnNames;
public boolean updataEmp(String sql,String []data)
{
SqlOper so=new SqlOper();
return so.updataExe(sql, data);
}
public void queryEmp(String sql,String []data)
{
SqlOper so = null;
columnNames=new Vector();
columnNames.add("员工编号");
columnNames.add("姓名");
columnNames.add("性别");
columnNames.add("家庭住址");
columnNames.add("出生日期");
columnNames.add("职位");
columnNames.add("电话号码");
columnNames.add("注册时间");
columnNames.add("备注");
rowData=new Vector();
try {
so=new SqlOper();
ResultSet rs=so.queryExe(sql, data);
while(rs.next())
{
Vector hang=new Vector();
hang.add(rs.getString(1));
hang.add(rs.getString(2));
hang.add(rs.getString(3));
hang.add(rs.getString(4));
hang.add(rs.getString(5));
hang.add(rs.getString(6));
hang.add(rs.getString(7));
hang.add(rs.getString(8));
hang.add(rs.getString(9));
rowData.add(hang);
}
} catch (Exception e) {
e.printStackTrace();
}finally
{
so.close();
}
}
//多少列
public int getColumnCount() {
return this.columnNames.size();
}
//多少行
public int getRowCount() {
return this.rowData.size();
}
@Override
public String getColumnName(int arg0) {
return (String)this.columnNames.get(arg0);
}
//某行某列数据
public Object getValueAt(int arg0, int arg1) {
return ((Vector) this.rowData.get(arg0)).get(arg1);
}
}