jdbc数据库连接,增删该查封装方法

package edu.tinzel.util;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
	 //声明Connection对象

//=================windows10==========

/******************sqlserver*************

    //驱动程序名

	private static String driver = "net.sourceforge.jtds.jdbc.Driver";

	private static String url = "jdbc:jtds:sqlserver://localhost:1433;DataBaseName=数据库名称";

    *******************************

*/

/******************mysql*************

   //驱动程序名

	private static String driver = "com.mysql.jdbc.Driver";

   //URL指向要访问的数据库名mydata
	private static String url = "jdbc:mysql://localhost:3306/数据库名称";

*******************************

*/

//===========================

   //MySQL配置时的用户名
	private static String user = "admin";
   //MySQL配置时的密码
	private static String password = "admin";
	
	private static Connection  con;
	private static PreparedStatement ps = null;
	private static ResultSet rs = null;
	//创建执行SQL语句对象
	 /**
	 * @param sql
	 * @param objects
	 * @return
	 */
	private static PreparedStatement createPreparedStatament(String sql,Object... objects) {
		try {
			Class.forName(driver);
            con = DriverManager.getConnection(url,user,password);
//			con = DriverManager.getConnection(url,"sa","123");
			ps = con.prepareStatement(sql);
			for (int i = 0; i < objects.length; i++) {
				ps.setObject(i+1,objects[i]);
			}
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ps;
	}
	//增删改
	//DriverManager
	// Connection
	//PreparedStatement
	/**
	 * 增删改
	 * @param sql
	 * @param objects
	 * @return
	 * @throws Exception
	 */
	public static boolean executeUpdate(String sql,Object... objects) throws Exception {
		boolean bo = false;
		ps = createPreparedStatament(sql,objects);
		bo = ps.executeUpdate() != 0? true:false;
		close(con,ps,null);
		return bo;
	}
	/**
	 * 登录
	 * @param name
	 * @param pwd
	 * @param sql
	 * @return
	 * @throws Exception
	 */
	public static boolean excuteExecute(String name,String pwd,String sql) throws Exception {
		boolean bo = false;
		ps = createPreparedStatament(sql, name,pwd);
		rs = ps.executeQuery();
		bo = rs.next();
		close(con,ps,rs);
		return bo;
	}
	
	//查询
	 // DriverManager
	//Connection
	 // PreparedStatement
	 //ResultSet
	/**
	 * 查询
	 * 注意:调用请关闭Connect PreparedStatement ResultSet 对象
	 */
	public static ResultSet executeQuery(String sql,Object... objects) throws Exception {
		ps = createPreparedStatament(sql,objects);
		rs = ps.executeQuery();
		return rs;
		
	}
	/**
	 * 关闭系统(有参)
	 */
	public static void close(Connection con,PreparedStatement ps,ResultSet rs) throws Exception {
		if (rs != null) {
			rs.close();
			rs = null;
		}
		if (ps != null) {
			ps.close();
			ps = null;
		}
		if (con != null) {
			con.close();
			con = null;
		}
	}
	/**
	 * 关闭系统(无参)
	 */
	public static void close() throws Exception {
		if (rs != null) {
			rs.close();
			rs = null;
		}
		if (ps != null) {
			ps.close();
			ps = null;
		}
		if (con != null) {
			con.close();
			con = null;
		}
	}
}

            
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页