JDBC的配置以及简单功能的实现

1、JDBC简介

1)什么是JDBC(java Database Connectivity)java连接数据库技术
        SUN推出的针对于关系型数据库开发的一整套规范(一套执行sql语句的API),主要由接口组成,目的是为了提升开发效率。
每个数据库产商的驱动程序,必须实现该接口。
开发者只需要学习jdbc的接口,就可以实现不同数据库的统一操作
2)JDBC需要的jar包
        java.sql
        javax.sql
        数据库驱动包(数据库的实现类,由各个数据库厂商提供)

中间版

一、创建user类 即(数据库表)

public class User {
private String username;
private String password;
private int uid;
public String getUsername() {
	return username;
}
public void setUsername(String username) {
	this.username = username;
}
public String getPassword() {
	return password;
}
public void setPassword(String password) {
	this.password = password;
}

public User() {
	super();
}
public int getUid() {
	return uid;
}
public void setUid(int uid) {
	this.uid = uid;
}
public User(String username, String password, int uid) {
	super();
	this.username = username;
	this.password = password;
	this.uid = uid;
}
@Override
public String toString() {
	return "User [username=" + username + ", password=" + password + ", uid=" + uid + "]";
}

}

二、创建接口即功能接口

package dao;
/**
*数据持久层的接口(规范)
*dao-->dataa access object 
*/

import java.util.List;

import entity.User;

public interface UserDao {
	
	//查询所有
	List<User> queryAll();
	//添加
	int insert(User user);
	//修改
	int update(User user);
	//删除
	int delete (int uid) ;
	//根据id查询
	User findById(int uid);
	
}

三、创建实现功能的类即功能实现类

*/
//获取连接
//注册驱动
public class UserDaoImpl implements UserDao{
	Connection conn = null ;
	PreparedStatement ps = null;
	ResultSet rs= null;
	@Override
	public List<User> queryAll() {
		List<User> users = new ArrayList<User>();
		try {
			conn=JdbcUtils.getConnection();
			String sql = "select*from user ";
			 ps = conn.prepareStatement(sql);
			 rs = ps.executeQuery();
			 while (rs.next()) {
				String username = rs.getString("username");
				String password = rs.getString("password");
				int uid = rs.getInt("uid");
				User user = new User  (username,password,uid);
				users.add(user);
			}
		}  catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
		return users;
	}
	@Override
	public int insert(User user) {
		int rows = 0;
		try {
			conn=JdbcUtils.getConnection();
			String sql = "insert into user value(?,?,?)";
			 ps = conn.prepareStatement(sql);
			 ps.setString(1, user.getUsername());
			 ps.setString(2, user.getPassword());
			 ps.setInt(3, user.getUid());
			 rows = ps.executeUpdate();
		}  catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
		return rows;
	}

	@Override //改
	public int update(User user) {
		int rows = 0;
		try {
			conn=JdbcUtils.getConnection();	
			String sql = "update user set username = ?,password=? where uid=?";
			 ps = conn.prepareStatement(sql);
			 ps.setString(1, user.getUsername());
			 ps.setString(2, user.getPassword());
			 ps.setInt(1, user.getUid());
			 rows = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			
			JdbcUtils.close(conn, ps, rs);
		}
		return rows;
	}
	@Override//删除
	public int delete(int uid) {
		int rows = 0;
		try {
			conn=JdbcUtils.getConnection();			String sql = "delete from user where uid=?";
			 ps = conn.prepareStatement(sql);
			 ps.setInt(1, uid);
			 rows = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
		return rows;
	}

	@Override
	public User findById(int uid) {
		User user = null;
		List<User> users = new ArrayList<User>();
		try {
			conn=JdbcUtils.getConnection();
			String sql = "select*from user  where uid=?";
			 ps = conn.prepareStatement(sql);
			 ps.setInt(1, uid);
			rs =ps.executeQuery();
			while (rs.next()) {
				String username = rs.getString("username");
				String password = rs.getString("password");
				user = new User(username,password,uid);
			}
		}  catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
		return user;
	}
	
} 

四、根据第一版的功能实现类将代码进行化简
即创建util工具类

package util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
/**
*硬编码
*/
public class JdbcUtils {
 static String url = null;
 static String user =null;
 static String password = null;
	//1、获取连接
	//注册驱动? 1次
	static {
		try {
			Properties pros = new Properties();
			InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");//获取类加载器
			//加载映射对
			pros.load(is);
			is.close();
			//获取Value值
			url = pros.getProperty("url");
			user = pros.getProperty("user");
			password = pros.getProperty("password");
			//注册一次
			Class.forName(pros.getProperty("driver"));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//获取连接
	public  static Connection getConnection() {
		try {
			return DriverManager.getConnection(
					url,user,password);
		} catch (SQLException e) {
			throw new RuntimeException("获取连接失败");
		}
	}
	public static void close(Connection conn ,PreparedStatement ps,ResultSet rs) {
		try {
			if (rs!=null) {
				rs.close();
			}
			if (ps!=null) {
				ps.close();
			}
			if (conn!=null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

5、还有就是创建一个properties文件记录数据库信息

driver = com.mysql.jdbc.Driver
url = jdbc:mysql:///bd1906?userSSL= true 
user = root
password = root

6、测试类测试代码

public class Test {
public static void main(String[] args) {
	UserDao dao = new UserDaoImpl();
	System.out.println(dao.queryAll());
	System.out.println(dao.findById(2));
	System.out.println(dao.delete(2));
}
}

你可能感兴趣的:(JDBC的配置以及简单功能的实现)