一个实现单表增删查改的小程序.
下面代码用的是Customers表,有id(自动增长,主键),name,age,address字段
程序分为有 提供连接的ConnectionProvider类,用于保存记录的对象Customer类,产生界面的Viewer类,控制器Contral类
属性文件内容:文件名是db.conf
1.数据库
- #MySQL
- JDBC_DRIVER=com.mysql.jdbc.Driver
- URL=jdbc:mysql://localhost:3306/test
- USER=root
- PASSWORD=1234
2.提供连接的类 DAO
- package jdbc;
- import java.io.*;
- import java.sql.*;
- import java.util.*;
- public class ConnectionProvider {
- private static Connection con = null;
- private static Properties ps = new Properties();
- static{
- try {
- InputStream in = new FileInputStream("db.conf");
- ps.load(in);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static Connection getConnection(){
- String JDBC_DRIVER = ps.getProperty("JDBC_DRIVER");
- String URL = ps.getProperty("URL");
- String USER = ps.getProperty("USER");
- String PASSWORD = ps.getProperty("PASSWORD");
- try {
- Class.forName("com.mysql.jdbc.Driver");
- DriverManager.registerDriver(new com.mysql.jdbc.Driver());
- con = DriverManager.getConnection(URL,USER,PASSWORD);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return con;
- }
- }
3.视图类 (表示层)
- package jdbc;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- /**视图类,用来展示记录,和将信息反馈给控制器,并接受和显示控制器返回的结果*/
- public class Viewer implements ActionListener{
- private JFrame jf;
- private JLabel topLabel;
- private JTextField tableField;
- private JTextField idField;
- private JTextField nameField;
- private JTextField ageField;
- private JTextField addressField;
- private Contral contral;
- private Customer customer;
- public Viewer(){
- buildViewer();
- contral = new Contral();
- customer = contral.first();
- show(customer,contral.getSize());
- }
- public void buildViewer(){
- jf = new JFrame();
- topLabel = new JLabel(" 第0条记录,一共有0条记录");
- jf.add(topLabel,BorderLayout.NORTH);
- JPanel jpCenter = new JPanel(new GridLayout(4,2));
- JLabel idLabel = new JLabel("id");
- JLabel nameLabel = new JLabel("name");
- JLabel ageLabel = new JLabel("age");
- JLabel addressLabel = new JLabel("address");
- idField = new JTextField(20);
- nameField = new JTextField(20);
- ageField = new JTextField(20);
- addressField = new JTextField(20);
- jpCenter.add(idLabel);
- jpCenter.add(idField);
- jpCenter.add(nameLabel);
- jpCenter.add(nameField);
- jpCenter.add(ageLabel);
- jpCenter.add(ageField);
- jpCenter.add(addressLabel);
- jpCenter.add(addressField);
- jf.add(jpCenter);
- JPanel jpSouth = new JPanel();
- JButton first = new JButton("first");
- JButton previous = new JButton("previous");
- JButton next = new JButton("next");
- JButton last = new JButton("last");
- JButton insert = new JButton("insert");
- JButton update = new JButton("update");
- JButton dalete = new JButton("dalete");
- JButton refresh = new JButton("refresh");
- first.addActionListener(this);
- previous.addActionListener(this);
- next.addActionListener(this);
- last.addActionListener(this);
- insert.addActionListener(this);
- update.addActionListener(this);
- dalete.addActionListener(this);
- refresh.addActionListener(this);
- jpSouth.add(first);
- jpSouth.add(previous);
- jpSouth.add(next);
- jpSouth.add(last);
- jpSouth.add(insert);
- jpSouth.add(update);
- jpSouth.add(dalete);
- jpSouth.add(refresh);
- jf.add(jpSouth,BorderLayout.SOUTH);
- jf.setSize(600,200);
- jf.setLocation(200, 200);
- jf.setVisible(true);
- jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- /**展示记录*/
- public void show(Customer customer,String size){
- idField.setText(customer.getId() + "");
- nameField.setText(customer.getName());
- ageField.setText(customer.getAge() + "");
- addressField.setText(customer.getAddress());
- topLabel.setText(" 第"
- + size.split(":")[0]
- +"条记录,一共有"+size.split(":")[1]+"条记录");
- }
- /**事件监听*/
- public void actionPerformed(ActionEvent e) {
- String comm = e.getActionCommand();
- if("first".equals(comm)){
- customer = contral.first();
- show(customer,contral.getSize());
- }else if("previous".equals(comm)){
- customer = contral.previous();
- show(customer,contral.getSize());
- }else if("next".equals(comm)){
- customer = contral.next();
- show(customer,contral.getSize());
- }else if("last".equals(comm)){
- customer = contral.last();
- show(customer,contral.getSize());
- }else if("insert".equals(comm)){
- String name = nameField.getText();
- int age = Integer.parseInt(ageField.getText());
- String address = addressField.getText();
- customer = contral.insert(name,age,address);
- show(customer,contral.getSize());
- }else if("update".equals(comm)){
- String name = nameField.getText();
- int age = Integer.parseInt(ageField.getText());
- String address = addressField.getText();
- contral.update(name,age,address);
- }else if("dalete".equals(comm)){
- customer = contral.dalete();
- show(customer,contral.getSize());
- }else if("refresh".equals(comm)){
- customer = contral.refresh();
- show(customer,contral.getSize());
- }
- }
- public static void main(String[] args) {
- new Viewer();
- }
- }
4.控制器类(逻辑层)
- package jdbc;
- import java.sql.*;
- import java.util.*;
- public class Contral {
- private int cur = 0;
- private Connection con = null;
- private Statement stmt = null;
- private ResultSet rs = null;
- private ArrayList<Customer> customers;
- /**构造,产生对象结果集合*/
- public Contral(){
- buildCustomersList();
- }
- /**执行查询语句,将结果集转换成对象集*/
- private void buildCustomersList(){
- customers = new ArrayList<Customer>();
- con = ConnectionProvider.getConnection();
- try {
- stmt = con.createStatement();
- rs = stmt.executeQuery("select * from customers");
- customers = new ArrayList<Customer>();
- while(rs.next()){
- int id = rs.getInt(1);
- String name = toGB(rs.getString(2));
- int age = rs.getInt(3);
- String address = toGB(rs.getString(4));
- Customer customer = new Customer(id,name,age,address);
- customers.add(customer);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- closeConnection(con);
- closeStatement(stmt);
- closeResultSet(rs);
- }
- }
- /**显示第一个元素*/
- public Customer first(){
- cur = 0;
- return customers.get(cur);
- }
- /**显示前一个元素*/
- public Customer previous(){
- if(cur>0)
- cur--;
- return customers.get(cur);
- }
- /**显示下一个*/
- public Customer next(){
- if(cur<customers.size()-1)
- cur++;
- return customers.get(cur);
- }
- /**显示最后一个*/
- public Customer last(){
- cur = customers.size()-1;
- return customers.get(cur);
- }
- /**插入记录*/
- public Customer insert(String name,int age,String address){
- Customer customer;
- String sql = "insert into customers (name,age,address)value("
- + "'" + toISO(name) + "'" + ","
- + age + ","
- + "'" + toISO(address) + "'" + ")";
- boolean isOk = exe(sql);
- try{
- if(isOk){
- con = ConnectionProvider.getConnection();
- stmt = con.createStatement();
- sql = "select id from customers where name="
- + "'" + toISO(name) + "'";
- rs = stmt.executeQuery(sql);
- rs.next();
- customer = new Customer(rs.getInt(1),name,age,address);
- customers.add(cur,customer);
- return customer;
- }else{
- return customers.get(cur);
- }
- }catch(Exception e){
- e.printStackTrace();
- return customers.get(cur);
- }finally{
- closeConnection(con);
- closeStatement(stmt);
- }
- }
- /**记录更新*/
- public void update(String name,int age,String address){
- Customer customer = customers.get(cur);
- customer.setName(name);
- customer.setAge(age);
- customer.setAddress(address);
- String sql = "update customers set name=" + "'" + toISO(name) + "'" + ","
- + "age=" + age + ","
- + "address=" +"'" + toISO(address) + "'"
- + "where id=" + customer.getId();
- exe(sql);
- }
- /**删除记录*/
- public Customer dalete(){
- Customer customer = customers.get(cur);
- String sql = "delete from customers where id=" + customer.getId();
- exe(sql);
- customers.remove(cur);
- if(cur<customers.size()-1){
- customer = customers.get(cur);
- }else{
- cur = 0;
- customer = customers.get(cur);
- }
- return customer;
- }
- /**刷新结果集合*/
- public Customer refresh(){
- buildCustomersList();
- return first();
- }
- /**用于执行SQL语句*/
- private boolean exe(String sql){
- con = ConnectionProvider.getConnection();
- try {
- stmt = con.createStatement();
- stmt.execute(sql);
- return true;
- } catch (SQLException e) {
- e.printStackTrace();
- return false;
- }finally{
- closeConnection(con);
- closeStatement(stmt);
- closeResultSet(rs);
- }
- }
- /**获取总记录数和当前记录数的字符串表示*/
- public String getSize(){
- return (cur+1) + ":" + customers.size();
- }
- /**把gb2312转化成ISO-8859-1*/
- private String toISO(String str){
- try {
- return str = new String(str.getBytes("GB2312"),"ISO-8859-1");
- } catch (Exception e) {e.printStackTrace();}
- return str;
- }
- /**把ISO-8859-1转化成gb2312*/
- private String toGB(String str){
- try {
- return str = new String(str.getBytes("ISO-8859-1"),"GB2312");
- } catch (Exception e) {e.printStackTrace();}
- return str;
- }
- /**关闭连接*/
- private void closeConnection(Connection con){
- try{if(con!=null)con.close();}catch(Exception e){e.printStackTrace();}
- }
- /**关闭语句*/
- private void closeStatement(Statement stmt){
- try{if(stmt!=null)stmt.close();}catch(Exception e){e.printStackTrace();}
- }
- /**关闭结果集*/
- private void closeResultSet(ResultSet rs){
- try{if(rs!=null)rs.close();}catch(Exception e){e.printStackTrace();}
- }
- }
5.POJO 基本的bean类
- package jdbc;
- public class Customer {
- private int id;
- private String name;
- private int age;
- private String address;
- public Customer(int id,String name,int age,String address){
- this.id = id;
- this.name = name;
- this.age = age;
- this.address = address;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- }