JDBC连接数据库的步骤

  1. 在项目的根目录>创建Folder(文件夹)命名lib,然后导入jar文件.
  2. 右击jar文件>Build Path(构建路径)>Add to Build Path,此时Referenced Libraries(引用库)下就有了jar包.
  3. 点击Fefereced LIbraries>找到com.mysql.jdbc(包)>Driver.class右击Copy Qualified Name(复制限定名)全路径com.mysql.jdbc.Driver
  4. src(文件夹)>创建File(文件)命名database.properties(配置文件)
namevalue
drivercom.mysql.jdbc.Driver
urljdbc:mysql://localhost:3306/myschool(数据库)
userNameroot(用户名)
password123(密码)
/**
*连接数据类
/

/*配置文件database.properties
    driver=com.mysql.jdbc.Driver;
    url=jdbc:mysql://localhost:3306/数据库名
    userName="root";
    password="123";*/

public class BaseDao {
	public static String driver;
	public static String url;
	public static String username;
	public static String password;
	public static Connection conn;
	public static PreparedStatement prepareStatement;
	public static ResultSet resultSet;
	
    /*
    *读取配置文件(静态代码块只执行一次)
    */
    static {
		//获取配置文件的全路径
		String path="database.properties";
		//将配置文件加到到输入流中
		InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(path);
		//创建读取配置文件的对象properties
		Properties properties=new Properties();
		try {
			//加载输入流
			properties.load(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//通过key取value值
		url=properties.getProperty("url");
		username=properties.getProperty("username");
		password=properties.getProperty("password");
		
	}

	/*
    *返回Connection对象
    */
	public static Connection getConn(){
		try {
			//反射加载当前数据库的驱动
			Class.forName(driver);
			//通过驱动管理器获得connection对象
			conn=DriverManager.getConnection(url,username,password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	/*
    *查询的方法(返回ResultSet结果集)
    */
	public static ResultSet select(String sql, Object... objects) {
		conn=getConn();
		try {
			prepareStatement=conn.prepareStatement(sql);
			if (objects!=null) {
				for (int i = 0; i < objects.length; i++) {
					prepareStatement.setObject(i+1, objects[i]);
				}
			}
			resultSet=prepareStatement.executeQuery();
		} catch (Exception e) {
		e.printStackTrace();
		}
		return resultSet;
	}

	/*
    *增删改的方法(返回false表示成功)
    */
	public static boolean update(String sql, Object... objects) {
		conn = getConn();
		boolean f = true;
		try {
			prepareStatement = conn.prepareStatement(sql);
			if (objects != null) {
				for (int i = 0; i < objects.length; i++) {
					prepareStatement.setObject(i + 1, objects[i]);
				}
			}
			f = prepareStatement.execute();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return f;
	}

    /*
    *增删改的方法(返回受影响的行数)
    */
	public static int update(String sql, Object... objects) {
		conn = getConn();
		int count=0;
		try {
			prepareStatement = conn.prepareStatement(sql);
			if (objects != null) {
				for (int i = 0; i < objects.length; i++) {
					prepareStatement.setObject(i + 1, objects[i]);
				}
			}
			count = prepareStatement.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	/*
    *关闭连接(先开后关的原则)
    */
	public void clo() {
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (prepareStatement != null) {
			try {
				prepareStatement.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
  1.  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值