zoukankan      html  css  js  c++  java
  • Java课程设计---创建数据库工具类

    1、传统的数据库操作

    package com.java.mysql;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    /**
     * <p>
     * Title: db.java
     * @author daxiang
     * @version 1.0 创建时间:2018年5月22日 上午8:00:22
     */
    public class Add {
    	public static void main(String[] args) {
    		Connection con;// 声明Connection对象
    		String driver = "com.mysql.jdbc.Driver";// 驱动程序名
    		String url = "jdbc:mysql://localhost:3306/salary";// URL指向要访问的数据库名mysql
    		String user = "root";// MySQL配置时的用户名
    		String password = "123";// MySQL配置时的密码
    		try {
    			Class.forName(driver);// 加载驱动程序
    			con = DriverManager.getConnection(url, user, password);// 使用getConnection()方法,连接MySQL数据库!!
    			if (!con.isClosed())
    				System.out.println("成功连接mysql数据库");
    			// 2.创建statement类对象,用来执行SQL语句!!
    			Statement statement = con.createStatement();
    			// 要执行的SQL语句
    			String sql = "insert into admin (id,username,password) values(2,'test','123')";
    			statement.execute(sql);
    			statement.close();
    			con.close();
    		} catch (ClassNotFoundException e) {
    			System.out.println("无法加载驱动");
    			e.printStackTrace();// 数据库驱动类异常处理
    		} catch (SQLException e) {
    			e.printStackTrace();// 数据库连接失败异常处理
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }

    2、自己封装db工具DbUtil.java

    由于增加、删除、更新在代码上存在大量的重复,所以现将数据库操作作为工具独立出来

    package com.system.utils;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import com.system.common.AppConstants;
    
    /**
     * 数据库连接类
     * 
     * @author daxiang
     *
     */
    public class DbUtil {
    
    	// 定义连接对象
    	private Connection connection;
    	private Statement statement;
    
    	/**
    	 * 
    	 * @throws Exception
    	 */
    	public DbUtil() throws Exception {
    		this.connection = getCon();
    		this.statement = connection.createStatement();
    	}
    
    	/**
    	 * 获取数据库连接
    	 * 
    	 * @return 数据库连接Connection
    	 * @throws Exception
    	 */
    	public Connection getCon() throws Exception {
    		Class.forName(AppConstants.JDBC_DRIVER);
    		Connection con = DriverManager.getConnection(AppConstants.JDBC_URL, AppConstants.JDBC_USERNAME,
    				AppConstants.JDBC_PASSWORD);
    		return con;
    	}
    
    	/**
    	 * 关闭数据库连接
    	 * 
    	 * @param con
    	 * @throws SQLException
    	 */
    	public void closeCon(Connection con) throws SQLException {
    		if (con != null) {
    			con.close();
    		}
    	}
    
    	/**
    	 * 更新
    	 * 
    	 * @param sql
    	 * @return
    	 * @throws SQLException
    	 */
    	public boolean update(String sql) throws SQLException {
    		return statement.execute(sql);
    	}
    
    	/**
    	 * 添加
    	 * 
    	 * @param sql
    	 * @return
    	 * @throws SQLException
    	 */
    	public boolean add(String sql) throws SQLException {
    		return statement.execute(sql);
    	}
    
    	/**
    	 * 删除
    	 * 
    	 * @param sql
    	 * @return
    	 * @throws SQLException
    	 */
    	public boolean delete(String sql) throws SQLException {
    		return statement.execute(sql);
    	}
    
    	/**
    	 * 查询
    	 * @param sql
    	 * @return
    	 * @throws SQLException
    	 */
    	public ResultSet query(String sql) throws SQLException{
    		return statement.executeQuery(sql);
    	}
    	
    }
    

      将可能修改的参数独立到一个AppConstants类中,便于修改

    /**
     * 项目名:student
     * 作者:daxiang
     * 时间:2018.6.1
     */
    package com.system.common;
    
    /**
     * 模块说明: 常量
     * 
     */
    public class AppConstants {
    	// jdbc
    	public static final String JDBC_URL = "jdbc:mysql://127.0.0.1:3306/db_student?useUnicode=true&characterEncodeing=UTF-8";
    	public static final String JDBC_USERNAME = "root";
    	public static final String JDBC_PASSWORD = "123";
    	public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    
    	// login view
    	public static final String LOGIN_TITLE = "登录界面";
    	public static final String LOGIN_USERNAME = "用户名";
    	public static final String LOGIN_PASSWORD = "密码";
    	public static final String LOGIN = "登录";
    	public static final String RESET = "重置";
    
    }
    

      

  • 相关阅读:
    学习PyQt5(二):PyQt5布局管理
    学习PyQt5(一):安装PyQt5以及在PyCharm上配置PyQt5
    Day037--Python--线程的其他方法,GIL, 线程事件,队列,线程池,协程
    Day036--Python--线程
    Day035--Python--管道, Manager, 进程池, 线程切换
    Day034--Python--锁, 信号量, 事件, 队列, 生产者消费者模型, joinableQueue
    Day033--Python--进程
    Day032--Python--操作系统, process进程
    Day30--Python--struct, socketserver
    Day29--Python--缓冲区, 粘包
  • 原文地址:https://www.cnblogs.com/daxiang2008/p/9172248.html
Copyright © 2011-2022 走看看