02javaweb基础:mysql数据库功能实现类

1:包结构

02javaweb基础:mysql数据库功能实现类_第1张图片

2:mysql驱动下载

    http://mvnrepository.com/artifact/mysql/mysql-connector-java

3:测试用例

public class test {

	public static void main(String[] args) {
		MysqlDemo.find("select * from user");
		MysqlDemo.add("insert into user values (null,'zhh')");
		MysqlDemo.delete("delete from user where username='zhh'");
		MysqlDemo.update("update user set username='zhh' where userid=3");
	}

}

4:文件源码

3.1:mysql.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=mysql用户名
password=mysql密码
3.2:JDBCUtils.java
package com.mysql.util;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtils {
	private static Properties prop = null;
	private JDBCUtils() {
	}
	static{
		try{
			prop = new Properties();
			prop.load(new FileReader(JDBCUtils.class.getClassLoader().getResource("mysql.properties").getPath()));
		}catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 获取连接
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 */
	public static Connection getConn() throws ClassNotFoundException, SQLException{
		// 1.注册数据库驱动
		Class.forName(prop.getProperty("driver"));
		// 2.获取连接
		return DriverManager.getConnection(prop.getProperty("url"), prop.getProperty("user"), prop.getProperty("password"));
		
	}
	/**
	 * 关闭连接
	 */
	public static void close(ResultSet rs, Statement stat,Connection conn){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				rs = null;
			}
		}
		if(stat!=null){
			try {
				stat.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				stat = null;
			}
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				conn = null;
			}
		}
	
	}
}
3.3:MysqlDemo.java
package com.mysql.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @description : mysql数据库功能实现类
 * @author : Mr_z
 * @time : 2018年3月29日下午5:06:19
 */
public class MysqlDemo {
	/**
	 * @description : 查询功能
	 * @param : select_sqlstr
	 * @return
	 */
	public static void find(String select_sqlstr){
		Connection conn = null;
		Statement stat = null;
		ResultSet rs = null;
		try{
			conn = JDBCUtils.getConn();
			stat =  conn.createStatement();
			rs = stat.executeQuery(select_sqlstr);
			while(rs.next()){
				String id = rs.getString("userid");
				String name = rs.getString("username");
				System.out.println(id+":"+name);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			JDBCUtils.close(rs, stat, conn);
		}
	}
	/**
	 * @description : 增加功能
	 * @param : insert_sqlstr
	 * @return
	 */
	public static void add(String insert_sqlstr) {
		Connection conn = null;
		Statement stat = null;
		try {
			// 1.注册数据库驱动
			// 2.获取连接
			conn = JDBCUtils.getConn();
			// 3.获取传输器对象
			stat = conn.createStatement();
			// 4.执行sql语句
			int count = stat
					.executeUpdate(insert_sqlstr);
			// 5.处理结果
			if (count > 0) {
				System.out.println("执行成功!影响到的行数为" + count);
			} else {
				System.out.println("执行失败!!");
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//6.关闭资源
			JDBCUtils.close(null, stat, conn);
		}
	}
	/**
	 * @description : 删除功能
	 * @param : delete_sqlstr
	 * @return
	 */
	public static void delete(String delete_sqlstr){
		Connection conn = null;
		Statement stat = null;
		ResultSet rs = null;
		try{
			conn = JDBCUtils.getConn();
			stat =  conn.createStatement();
			stat.executeUpdate(delete_sqlstr);
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			JDBCUtils.close(rs, stat, conn);
		}
	}
	/**
	 * @description : 修改功能
	 * @param : update_sqlstr
	 * @return
	 */
	public static void update(String update_sqlstr) {
		Connection conn = null;
		Statement stat = null;
		try{
			conn = JDBCUtils.getConn();
			stat =  conn.createStatement();
			stat.executeUpdate(update_sqlstr);
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			JDBCUtils.close(null, stat, conn);
		}
	}
}

你可能感兴趣的:(02javaweb基础:mysql数据库功能实现类)