JDBC----PrepareStatement


package java3.crud;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.junit.Test;

import java3.util.JDBCUtils;

public class PreparedStatementUpdateTest {
	
	@Test
	public void testCommonUpdate(){
//		String sql="delete from customers where id=?";
//		update(sql,3);
		//order这个表名和关键字冲突,要用``包起来
		String sql="update `order` set order_name=? where order_id=?";
		update(sql,"qq",2);
	}
	
	//通用的增删改操作
	//sql中占位符的个数与可变形参的长度相同
	public void update(String sql,Object ...args)  {
		Connection conn=null;
		PreparedStatement ps=null;
		try {
			//1.获取数据库的连接
			conn = JDBCUtils.getConnedtion();
			//2.预编译sql语句,返回PrepareStatement的实例
			ps = conn.prepareStatement(sql);
			//3.填充占位符
			for(int i=0;i
package java3.crud;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import org.junit.Test;

import java3.bean.Customer;
import java3.util.JDBCUtils;

/*
 * 	针对于Customer表的查询操作
 */
public class CustomerForQuery {
	
	@Test
	public void testQueryForCustomers() throws Exception{
		String sql="select id,name,birth,email from customers where id=?";
		Customer customer = queryForCustomers(sql, 13);
		System.out.println(customer);
		
		sql="select name,email from customers where name=?";
		Customer queryForCustomers = queryForCustomers(sql, "周杰伦");
		System.out.println(queryForCustomers);
		
	}
	
	/*
	 * 针对Customers表的通用查询操作
	 */
	public Customer queryForCustomers(String sql,Object...args) throws Exception{
		
		Connection conn = JDBCUtils.getConnedtion();
		PreparedStatement ps = conn.prepareStatement(sql);
		for(int i=0;i
package java3.crud;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Date;

import org.junit.Test;

import java3.bean.Order;
import java3.util.JDBCUtils;

/*
 * 	针对于order表的通用的查询操作
 */
public class OrderForQuery {
	/*
	 * 	针对于表的字段与类的属性名不相同的情况下
	 * 1.必须声明sql时,使用类的属性名来命名字段的别名
	 * 2.使用ResultSetMetaData时,需要使用getColumnLabel()来替换getColumnName()来获取列的别名
	 * 说明:如果sql中没有给字段其别名,getColumnLabel()获取的就是列名
	 */
	
	
	
	@Test
	public void testOrderForQuery()  {
		String sql="select order_id orderId,order_name orderName,order_date orderDate from `order` where  order_id=?";
		Order orderForQuery = orderForQuery(sql, 1);
		System.out.println(orderForQuery);
		
	}
	
	/*
	 * 	通用的针对于order表的查询操作
	 */
	public Order orderForQuery(String sql,Object ...args)  {
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			conn = JDBCUtils.getConnedtion();
			ps = conn.prepareStatement(sql);
			for(int i=0;i
package java3.crud;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import java3.bean.Customer;
import java3.bean.Order;
import java3.util.JDBCUtils;
/*
使用PreparedStatement实现针对于不同表的通用的查询操作
*/
public class PrepareStatementQueryTest {
	
	@Test
	public void testGetForList(){
		String sql="select id,name,email from customers where id < ?";
		List forList = getForList(Customer.class, sql, 12);
//		System.out.println(forList);
		forList.forEach(System.out::println);
		
		System.out.println();
		String sql1="select order_id orderId,order_name orderName from `order`";
		List forList2 = getForList(Order.class, sql1);
		forList2.forEach(System.out::println);
		
		
	}
	
	
	public  List getForList(Class clazz,String sql,Object ...args){
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			conn = JDBCUtils.getConnedtion();
			ps = conn.prepareStatement(sql);
			for(int i=0;i list = new ArrayList();
			while(rs.next()){
				T t=clazz.newInstance();
				
				for(int i=0;i T getInstance(Class clazz,String sql,Object ...args)  {
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			conn = JDBCUtils.getConnedtion();
			ps = conn.prepareStatement(sql);
			for(int i=0;i
package java3.bean;

import java.util.Date;

/*
 * 		ORM编程思想(object relational mapping)
 * 	一个数据表对应一个java类
 * 	表中的一条记录对应java类的一个对象
 * 	表中的一个字段对应java类的一个属性
 */
public class Customer {
	private int id;
	private String name;
	private String email;
	private Date birth;
	public Customer() {
		super();
	}
	public Customer(int id, String name, String email, Date birth) {
		super();
		this.id = id;
		this.name = name;
		this.email = email;
		this.birth = birth;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Date getBirth() {
		return birth;
	}
	public void setBirth(Date birth) {
		this.birth = birth;
	}
	@Override
	public String toString() {
		return "Customer [id=" + id + ", name=" + name + ", email=" + email + ", birth=" + birth + "]";
	}
	
	
}

package java3.bean;

import java.util.Date;

public class Order {
	private int orderId;
	private String orderName;
	private Date orderDate;
	public Order() {
		super();
	}
	public Order(int orderId, String orderName, Date orderDate) {
		super();
		this.orderId = orderId;
		this.orderName = orderName;
		this.orderDate = orderDate;
	}
	public int getOrderId() {
		return orderId;
	}
	public void setOrderId(int orderId) {
		this.orderId = orderId;
	}
	public String getOrderName() {
		return orderName;
	}
	public void setOrderName(String orderName) {
		this.orderName = orderName;
	}
	public Date getOrderDate() {
		return orderDate;
	}
	public void setOrderDate(Date orderDate) {
		this.orderDate = orderDate;
	}
	@Override
	public String toString() {
		return "Order [orderId=" + orderId + ", orderName=" + orderName + ", orderDate=" + orderDate + "]";
	}
	
	
}

package java3.util;

import java.io.IOException;
import java.io.InputStream;
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 {
	//获取数据库的连接
	public static Connection getConnedtion() throws Exception{
		//1.读取配置文件中的4个基本信息
		 InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
		 Properties pros = new Properties();
		 pros.load(is);
		 
		 String user=pros.getProperty("user");
		 String password=pros.getProperty("password");
		 String url=pros.getProperty("url");
		 String driverClass=pros.getProperty("driverClass");
		 
		 //2.加载驱动
		 Class.forName(driverClass);
		 
		 //3.获取连接
		 Connection conn = DriverManager.getConnection(url, user, password);
		 return conn;
		
	}
	
	//关闭连接和statement的操纵
	public static void closeResource(Connection conn,Statement ps){
		if(ps!=null){
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	//关闭带结构集的资源的操作
	public static void closeResource(Connection conn,Statement ps,ResultSet rs){
		if(ps!=null){
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
}