JAVA利用Access驱动连接数据库进行增删查改并且写入txt文本

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

class AccessK{
	public static Connection conn=null;
	public static Statement stmt=null;
	public static ResultSet rs=null;
	public static Connection connect() {
		try {
			Class.forName("com.hxtt.sql.access.AccessDriver");
			String url="jdbc:Access:///d:/stu.accdb";
			Connection conn=DriverManager.getConnection(url, "", "");
			return conn;
		}catch(ClassNotFoundException e) {
			e.printStackTrace();
		}catch(SQLException e) {	
			e.printStackTrace();
		}catch(Exception e1) {
			e1.printStackTrace();
		}
		return null;
	}
	public static void update(String sql) {
		try {
			conn=connect();
			stmt=conn.createStatement();
			stmt.executeUpdate(sql);
		}catch(SQLException e) {
			e.printStackTrace();
		}catch(Exception e1) {
			e1.printStackTrace();
		}
	}
	public static ResultSet query(String sql) {
		try {
			conn=connect();
			stmt=conn.createStatement();
			rs=stmt.executeQuery(sql);
		}catch(SQLException e){
			e.printStackTrace();
		}
		return rs;
	}
	public static void close() {
		try {
			if(rs!=null)
				rs.close();
			if(stmt!=null)
				stmt.close();
			if(conn!=null)
				conn.close();
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}
}

class GUI implements ActionListener,ListSelectionListener{
	JFrame frame;
	JTable t;
	DefaultTableModel tm;
	JScrollPane sp;
	JPanel p1,p2,p3;
	JLabel l1,l2,l3,l4;
	JTextField t1,t2,t4;
	JRadioButton r1,r2;
	JButton b1,b2,b3,b4,b5,b6,b7;
	GUI(){
		frame=new JFrame("Tests");
		p1=new JPanel();p2=new JPanel();p3=new JPanel();
		tm=new DefaultTableModel();
		t=new JTable(tm);
		t.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		sp=new JScrollPane(t);
		sp.setPreferredSize(new Dimension(600,200));
		l1=new JLabel("学号:");l2=new JLabel("姓名:");
		l3=new JLabel("性别:");l4=new JLabel("爱好:");
		t1=new JTextField(10);t2=new JTextField(10);
		t4=new JTextField(10);
		r1=new JRadioButton("男",true);
		r2=new JRadioButton("女");
		ButtonGroup r=new ButtonGroup();
		r.add(r1);r.add(r2);
		b1=new JButton("增加");b2=new JButton("删除");
		b3=new JButton("修改");b4=new JButton("查找");
		b5=new JButton("显示全部");b6=new JButton("写入txt");
		b7=new JButton("清空文本框");
		frame.getContentPane().setLayout(new BorderLayout(2,2));
		frame.add(sp,BorderLayout.CENTER);
		p1.setLayout(new FlowLayout());
		p1.setSize(600, 20);
		p1.add(l1);p1.add(t1);p1.add(l2);p1.add(t2);
		p1.add(l3);p1.add(r1);p1.add(r2);p1.add(l4);p1.add(t4);
		p2.setLayout(new GridLayout(1,7));
		p2.add(b1);p2.add(b2);p2.add(b3);p2.add(b4);p2.add(b5);p2.add(b6);p2.add(b7);
		p3.setLayout(new GridLayout(2,1));
		p3.add(p1);p3.add(p2);
		frame.add(p3,BorderLayout.SOUTH);
		frame.setSize(600,300);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		showall();
		
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
		b5.addActionListener(this);
		b6.addActionListener(this);
		b7.addActionListener(this);
		t.getSelectionModel().addListSelectionListener(this);
	}
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==b1) insert();
		else if(e.getSource()==b2) delete();
		else if(e.getSource()==b3) update();
		else if(e.getSource()==b4) search();
		else if(e.getSource()==b5) showall();
		else if(e.getSource()==b6) write();
		else if(e.getSource()==b7) clear();
	}
	public void valueChanged(ListSelectionEvent e) {
		clear();
		int row = t.getSelectedRow();
		if(row>=0) {
			String id = (String) t.getValueAt(row, 0);
			String name = (String) t.getValueAt(row, 1);
			String sex = (String) t.getValueAt(row, 2);
			String aihao = (String) t.getValueAt(row, 3);
			t1.setText(id);t2.setText(name);
			if(sex.equals("男")) r1.setSelected(true);
			else r2.setSelected(true);
			t4.setText(aihao);
			}
	}
	public void insert() {
		String id = t1.getText();
		String name = t2.getText();
		String sex = "";
		if(r1.isSelected())
			sex="男";
		else
			sex="女";
		String aihao =t4.getText();
		if(id.length()<1) {
			JOptionPane.showMessageDialog(null,"请输入学号");
		}else {
			String sql ="select * from stu where 学号='"+id+"'";
			try {
				ResultSet rs = AccessK.query(sql);
				if(rs.next()) {
					JOptionPane.showMessageDialog(null,"学号已存在");
					AccessK.close();
				}else {
					AccessK.close();
					sql="insert into stu(学号,姓名,性别,爱好)values('"+id+"','"+name+"','"+sex+"','"+aihao+"')";
					AccessK.update(sql);
					AccessK.close();
					Vector<String> r=new Vector<String>();
					r.add(id);r.add(name);r.add(sex);r.add(aihao);
					tm.addRow(r);
					clear();
					JOptionPane.showMessageDialog(null, "增加成功!");
				}
			}catch(SQLException e) {
				e.printStackTrace();
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
	}
	public void delete() {
		int row = t.getSelectedRow();
		if(row==-1) {
			JOptionPane.showMessageDialog(null, "请选择要删除的行");
		}else {
			int res = JOptionPane.showConfirmDialog(null,"确定删除?","delete",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);
			if(res==JOptionPane.YES_OPTION) {
				String id = (String)t.getValueAt(row, 0);
				String sql = "delete from stu where 学号='"+id+"'";
				AccessK.update(sql);
				AccessK.close();
				tm.removeRow(row);
				JOptionPane.showMessageDialog(null, "删除成功!");
			}
		}
		clear();
	}
	public void update() {
		int row = t.getSelectedRow();
		if(row==-1) {
			JOptionPane.showMessageDialog(null,"请选择要修改的行");
		}
		else {
			String id = (String) t.getValueAt(row, 0);
			String name = t2.getText();
			String sex ="";
			if(r1.isSelected())
				sex="男";
			else
				sex="女";
			String aihao = t4.getText();
			String sql="update stu set 姓名='"+name+"',性别='"+sex+"',爱好='"+aihao+"'where 学号='"+id+"'";
			AccessK.update(sql);
			tm.setValueAt(id, row, 0);
			tm.setValueAt(name, row, 1);
			tm.setValueAt(sex, row, 2);
			tm.setValueAt(aihao, row, 3);
			AccessK.close();
			clear();
			JOptionPane.showMessageDialog(null, "修改成功");
		}
	}
	public void search() {
		String id = t1.getText();
		String name = t2.getText();
		String sex = "";
		if (r1.isSelected())
			sex = "男";
		else
			sex = "女";
		String aihao = t4.getText();
		String sql = "";
		if (aihao.equals("")) {
			sql = "select * from stu where 学号 like '%" + id + "%' and 姓名 like '%" + name + "%' and 性别 like '%" + sex
					+ "%'";
		} else {
			sql = "select * from stu where 学号 like '%" + id + "%' and 姓名 like '%" + name + "%' and 性别 like '%" + sex
					+ "%' and 爱好 like '%" + aihao + "%'";
		}
		try {
			Vector<String> vector = new Vector<String>();
			vector.add("学号");
			vector.add("姓名");
			vector.add("性别");
			vector.add("爱好");
			Vector<Vector<String>> data = new Vector<Vector<String>>();
			ResultSet rs = AccessK.query(sql);
			while (rs.next()) {
				String r_id = rs.getString("学号");
				String r_name = rs.getString("姓名");
				String r_sex = rs.getString("性别");
				String r_aihao = rs.getString("爱好");
				
				Vector<String> row = new Vector<String>();
				row.add(r_id);
				row.add(r_name);
				row.add(r_sex);
				row.add(r_aihao);
				data.add(row);
			}
			AccessK.close();
			tm.setDataVector(data, vector);
			clear();
			JOptionPane.showMessageDialog(null,"查找成功!");
			
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	public void showall() {
		String sql="select * from stu";
		Vector<String> v =new Vector<String>();
		v.add("学号");v.add("姓名");v.add("性别");v.add("爱好");
		Vector<Vector<String>> data = new Vector<Vector<String>>();
		try {
			ResultSet rs = AccessK.query(sql);
			while(rs.next()) {
				String id = rs.getString("学号");
				String name = rs.getString("姓名");
				String sex = rs.getString("性别");
				String aihao = rs.getString("爱好");
				
				Vector<String> r = new Vector<String>();
				r.add(id);r.add(name);r.add(sex);r.add(aihao);
				data.add(r);
			}
			AccessK.close();
			tm.setDataVector(data, v);
		}catch(SQLException e) {
			e.printStackTrace();
		}catch(Exception e) {
			e.printStackTrace();
		}
		clear();
	}
	public void write() {
		try {
			JFileChooser fileChooser=new JFileChooser("D:/");
			if(fileChooser.showOpenDialog(frame)==JFileChooser.APPROVE_OPTION) {
				File file=fileChooser.getSelectedFile();
				FileWriter f=new FileWriter(file);
				String str ="学号 姓名 性别 爱好\r\n";
				f.write(str);
				int row=t.getRowCount();
				for(int i=0;i<row;i++) {
					String id=(String)t.getValueAt(i, 0);
					String name=(String)t.getValueAt(i, 1);
					String sex= (String)t.getValueAt(i, 2);
					String aihao = (String)t.getValueAt(i, 3);
					str=id+" "+name+" "+sex+" "+aihao+"\r\n";
					f.write(str);
				}
				f.close();
				clear();
				JOptionPane.showMessageDialog(null,"写入成功!");
			}
		}catch(IOException ioe) {
		ioe.printStackTrace();
		}
	}
	public void clear() {
		t1.setText("");
		t2.setText("");
		r1.setSelected(true);
		t4.setText("");
	}
}

public class Tests {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		new GUI();
	}

}

你可能感兴趣的:(JAVA,Access,数据库,txt)