1. 程式人生 > >Java JDBC crud有參

Java JDBC crud有參

1.屬性類,儲存屬性以及set,get,toString方法

package java_n20.entity;

import java.util.Date;

/**
 * ClassName: Emp 
 * @Description: 屬性類
 * @author cai
 * @date 2018年10月23日 
 */
public class Emp {

	private Integer id;
	private String name;
	private String sex;
	private Date birth;
	private String departemnt;
	private String address;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	@Override
	public String toString() {
		return "Emp [id=" + id + ", name=" + name + ", sex=" + sex + ", birth=" + birth + ", departemnt=" + departemnt
				+ ", address=" + address + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
	public String getDepartemnt() {
		return departemnt;
	}
	public void setDepartemnt(String departemnt) {
		this.departemnt = departemnt;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
}

2.配置檔案

jdbc.name = root
jdbc.password = 2510.62
jdbc.url = jdbc:mysql://localhost/demo?useSSL=true&characterEncoding=utf8
jdbc.driver = com.mysql.jdbc.Driver

3.Mysql連線工具類

package java_n20.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * ClassName: DBHelper 
 * @Description: 獲得資料庫連線的工具類
 * @author cai
 * @date 2018年10月22日 
 */
public class DBHelper {
	
		//1.使用者名稱
	private static String name;
		//2.密碼
	private static String password;
		//3.地址
	private static String url;
		//4.驅動,從外部引進mysql-connector-jar檔案
	private static String driver;
	
	static {
		Properties prop = new Properties();
		try {
			//1.讀取jdbc.properties檔案的內容
			prop.load(new FileInputStream(new File("src/jdbc.properties")));
			name = prop.getProperty("jdbc.name");
			url = prop.getProperty("jdbc.url");
			password = prop.getProperty("jdbc.password");
			driver = prop.getProperty("jdbc.driver");
			//2.載入驅動
			Class.forName(driver);
		} catch (IOException e) {
					e.printStackTrace();
		} catch (ClassNotFoundException e) {
					e.printStackTrace();
		}			
	}
	public static Connection getDBConnection() {  //工具類方法一般為靜態,而且是不能例項化的
		Connection conn= null;
		try {
			//3.獲得連線
			conn= DriverManager.getConnection(url, name, password);
		} catch (SQLException e) { 	
			e.printStackTrace();
		}
		return conn;
	}	
	private DBHelper() {
		
	}	
}

4.介面類

package java_n20.dao;

import java.util.List;

import java_n20.entity.Emp;

/**
 * ClassName: EmpDao 
 * @Description: 介面,統一方法
 * @author cai
 * @date 2018年10月23日 
 */
public interface EmpDao {

	public Integer addEmp(Emp emp) throws Exception;  //因為傳輸的內容比較多,所以使用物件方便一些
	public Integer deleteEmp(int id) throws Exception;
	public Integer updateEmp(Emp emp) throws Exception;  //修改欄位的時候,要修改的數量並不一定,所以使用物件
	public Emp selectOneEmp(String name) throws Exception;
	public List<Emp> selectAll() throws Exception;
	public void selectByPage() throws Exception;
	public void selectCount() throws Exception;
}

5.介面實現類

package java_n20.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import java_n20.dao.EmpDao;
import java_n20.entity.Emp;
import java_n20.util.DBHelper;

/**
 * ClassName: EmpDaoImpl 
 * @Description: 介面實現類
 * @author cai
 * @date 2018年10月23日 
 */
public class EmpDaoImpl implements EmpDao{

	private Connection conn = null;
	private PreparedStatement ps = null;
	ResultSet rs =null;
	/**
	 * 新增員工  insert
	 */
	@Override
	public Integer addEmp(Emp emp) throws Exception {

		//1.獲得資料庫連線
		conn = DBHelper.getDBConnection();
		//2.準備sql語句
		String sql = "insert into student (id,name) values (?,?)";  
		//3.(預)編譯sql  Statement-->Preparement(預編譯物件,存放的是編譯後的sql語句)
		//Preparement 用於將引數化的sql語句傳送到資料庫
		//PreparedStatement表示預編譯的sql語句的物件,sql語句已預編譯並存儲在這個物件中
		ps = conn.prepareStatement(sql);
		//3.1如果有佔位符,此處給佔位符賦值
		ps.setInt(1,emp.getId());   //(佔位符位置,引數)  比如此處第一個佔位符
		ps.setString(2, emp.getName());
		//4.執行sql新增
		int num = ps.executeUpdate();  //返回值表示更改的記錄數,返回0一般表示失敗
		//5.關閉連線,釋放資源  
		conn.close();
		return num;
	}

	@Override
	public Integer deleteEmp(int id) throws Exception {
		conn = DBHelper.getDBConnection();
		String sql = "delete from student where id = ?";
		ps = conn.prepareStatement(sql);
		ps.setInt(1, id);
		//4.執行sql刪除
		int num = ps.executeUpdate();  
		conn.close();
		return num;
	}
	@Override
	public Integer updateEmp(Emp emp) throws Exception {
		conn = DBHelper.getDBConnection();
		String sql = "update student set name=? where id = ?";
		ps = conn.prepareStatement(sql);
		ps.setString(1, emp.getName());
		ps.setInt(2, emp.getId());
		//4.執行sql更改
		int num = ps.executeUpdate();  
		conn.close();
		return num;
	}
	@Override
	public Emp selectOneEmp(String na) throws Exception {
		conn = DBHelper.getDBConnection();
		String sql = "select * from student where name = ccc";
		ps = conn.prepareStatement(sql);
		ps.setString(1, na);
		//4.執行sql查詢
		rs = ps.executeQuery();  //返回一個結果集,是資料庫裡面查詢的資料資訊
		
		//4.1獲取返回的資料資訊
		Emp emp = new Emp();
		if(rs.next()) {  //指標下一行有沒有資料
			String name = rs.getString("name"); //引數為列資訊
			String sex = rs.getString("sex");
			emp.setName(name);
			emp.setSex(sex);
		}
		//5.關閉
		rs.close();
		ps.close();
		conn.close();
		return emp;
	}
	@Override
	public List<Emp> selectAll() throws Exception {
		conn = DBHelper.getDBConnection();
		String sql = "select * from student";
		ps = conn.prepareStatement(sql);
		//4.執行sql查詢
		rs = ps.executeQuery();  //返回一個結果集,是資料庫裡面查詢的資料資訊
		
		//4.1獲取返回的資料資訊
		List<Emp> empList=new ArrayList<>();
		while(rs.next()) {  //指標下一行有沒有資料
			String name = rs.getString("name"); //引數為列資訊
			String sex = rs.getString("sex"); 
			Emp emp = new Emp();
			emp.setName(name);
			emp.setName(sex);
			empList.add(emp);
		}
		//5.關閉
		rs.close();
		ps.close();
		conn.close();
		return null;
	}

	@Override
	public void selectByPage() throws Exception {
		conn = DBHelper.getDBConnection();
		String sql = "select * from student limit 0,5";
		ps = conn.prepareStatement(sql);
		//4.執行sql查詢
		rs = ps.executeQuery();  //返回一個結果集,是資料庫裡面查詢的資料資訊
		
		//4.1獲取返回的資料資訊
		while(rs.next()) {  //指標下一行有沒有資料
			String name = rs.getString("name"); //引數為列資訊
			String sex = rs.getString("sex");
			System.out.println(name+sex);
		}
		//5.關閉
		rs.close();
		ps.close();
		conn.close();
		
	}

	@Override
	public void selectCount() throws Exception {
		conn = DBHelper.getDBConnection();
		String sql = "select count(*) from student";
		ps = conn.prepareStatement(sql);
		//4.執行sql查詢
		rs = ps.executeQuery();  //返回一個結果集,是資料庫裡面查詢的資料資訊
		
		//4.1獲取返回的資料資訊
		while(rs.next()) {  //指標下一行有沒有資料
			int total = rs.getInt("count(*)");
			System.out.println(total);
		}
		//5.關閉
		rs.close();
		ps.close();
		conn.close();
		
	}
}

6.測試類

package java_n20.test;

import java_n20.dao.EmpDao;
import java_n20.dao.impl.EmpDaoImpl;
import java_n20.entity.Emp;

/**
 * ClassName: TestEmp 
 * @Description: 測試類
 * @author cai
 * @date 2018年10月23日 
 */
public class TestEmp {

	public static void main(String[] args) {
		EmpDao empDao = new EmpDaoImpl();
		//要傳輸的資料
		int id = 000;
		String name = "cc";
		Emp emp = new Emp();
		emp.setId(id);
		emp.setName(name);
		try {
			int num= empDao.addEmp(emp);
			if(num==1){
				System.out.println("新增成功");
			}
			empDao.updateEmp(emp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}