Java Swing JTable实现增删改查 DBUtils 工具类补充

DBUtils 工具类

看见很多同学,浏览了(Java Swing JTable 实现增删改查),这篇文章,想要运行一下看下效果,但是由于缺少DUtils 工具类,无法连接数据库进行测试,因此特地做出补充。

以下为DBUtils工具类的源码补充

import java.sql.*;

public class DBUtils {
	//URL的填写请自行百度,根据数据库版本搜索
    private final static String URL = "your url";
    
	//填写你的数据库连接用户名
	private final static String ROOT = "your name";
    
	//填写你的数据库连接密码
	private final static String PASSWORD = "your password";
    
	
	private static Connection conn;
    private static PreparedStatement ps;
    private static ResultSet rs;

	//加载驱动,获取连接
    public static Connection getConn() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
        return DriverManager.getConnection(URL, ROOT, PASSWORD);
    }

    /**
     * @param sql:执行的sql
     * @param params:设置参数
     * @return
     */
	 
	 //执行增删改语句,这里使用Object[]数组是为了满足不同参数传递的需要
    public static boolean executeUpdate(String sql, Object[] params){
        boolean flag = false;
        try {
            conn = getConn();
            ps = conn.prepareStatement(sql);//预编译SQL
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
            }
            int count = ps.executeUpdate();//执行SQL
            if (count > 0)
                flag = true;
            else
                flag = false;
            return flag;
        } catch (SQLException s) {
            s.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return flag;
    }


	//执行查询语句
    public static ResultSet executeQuery(String sql, Object[] params) {
        try {
            conn = getConn();
            ps = conn.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
            }
            rs = ps.executeQuery();
            return rs;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException s) {
            s.printStackTrace();
        }
        return rs;
    }
	
	
	//关闭流,由于参数个数不同,这里使用了重载
    public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection conn, PreparedStatement ps) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

	//这里对代码进行测试,测试数据库是否连接成功,没有输出就是最好的消息
    public static void main(String[] args) {
        try {
            DBUtils.getConn();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

package BookTest; import java.awt.*; import java.awt.event.*; import java.util.AbstractQueue; import java.util.Vector; import javax.swing.*; import javax.swing.table.*; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeSelectionModel; import javax.swing.event.*; import javax.swing.table.TableColumnModel; //import DbHelp.OperatorJobs; import Dao.ArrayBook; public class Test extends JFrame { public JPanel jp=null; public String aa; //JTable jTable1 = new JTable(); public JTable jTable1=null; private JTree jTree1; DefaultTableModel mode; private JScrollPane scrollPane = null; DefaultMutableTreeNode node; JButton[] btn={new JButton("增加"),new JButton("删除"),new JButton("修改"),new JButton("查询"),new JButton("重置")}; public JTextField[] txt ={new JTextField(),new JTextField(),new JTextField(),new JTextField(),new JTextField(),new JTextField()}; public String[] str1={"1","summer","boy","20","",""}; public String[] str2={"2","wulei","girl","19","",""}; public String[] str3={"3","west","boy","20","",""}; public String[] str4={"4","fish","girl","18","",""}; public String[] str5={"5","youku","boy","21","",""}; public String[] str6={"6","aa","boy","21","",""}; public String[][] tempData1={str1,str2,str3,str4,str5,str6}; public String[] aa1={"1","summer","boy","20","123","123"}; public String[] aa2={"2","wulei","girl","19","123",""}; public String[] aa3={"3","west","boy","20","123",""}; public String[] aa4={"4","fish","girl","18","1",""}; public String[] aa5={"5","youku","boy","21","12",""}; public String[] aa6={"6","aa","boy","21","123",""}; public String[][] tempData4={aa1,aa2,aa3,aa4,aa5,aa6}; //设置表头数据 public String[] tempTitle={"ID","标题","内容","作者","时间","是否选中"}; //设置表格的数据集合 Vector v1=new Vector(); Vecto
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值