18.ORM之封装到JavaBean对象(多条数据)

package jdbc;

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


public class Demo_main {
/*
 * ORM(Object Relationship Mapping)的基本思想
 * 表结构和类对应
 * 表中字段和类属性对应
 * 表中数据和对象对应
 */
/*	将表中的一条记录封装到JavaBean对象中
 * 		首先要创建类,类名对应表名,属性对应列名,要有构造方法和set,get方法,属性类型为private私有的
 */

//	
	public static void main(String[] args){
		PreparedStatement ps = null;
		Connection c = null;
		ResultSet rs = null;
		List<Demo_1> li = new ArrayList<Demo_1>();
			try {
//加载驱动类并建立连接
				c = Demo.getmysqlCon();		
//执行SQL语句													
				ps = c.prepareStatement("select * from Demo_1 where id<? ");
				ps.setObject(1, 4);
				rs = ps.executeQuery();
			//获取数据	
				while(rs.next()) {
					Demo_1 d1 = new Demo_1(rs.getInt(1), rs.getString(2), rs.getDouble(3), rs.getInt(4));
					li.add(d1);
				}
			//输出
				for(Demo_1 d1:li) {
					System.out.println(d1.getId()+"   "+d1.getName()+"   "+d1.getMoney()+"   "+d1.getCno());
				}

			} catch (Exception e) {
				e.printStackTrace();
			}finally {
//关闭
				Demo.closeBase(rs, ps, c);
			}
			
	
	}

}

工具类

package jdbc;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Properties;

public class Demo {
	static Properties p = null;//用于帮助我们读取和处理资源文件的信息
//获取资源文件数据
	static {
		p = new Properties();
		try {
			p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("JDBC.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
//加载驱动和获取连接的方法
	//Mysql
		public static Connection getmysqlCon() {
			try {
				Class.forName(p.getProperty("MysqlDriver"));
				return DriverManager.getConnection(p.getProperty("MysqlUrl"),p.getProperty("MysqlUser"),p.getProperty("MysqlPassworld"));		
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
//常用关闭的方法
		public static void closeBase(ResultSet rs,Statement s, Connection c) {
			try {
				if(s != null) {
					s.close();
					}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(c != null) {
					c.close();
					}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(rs != null) {
					rs.close();						
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
//时间转字符串
		public static long stringdate(String datestring) {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			try {
				return df.parse(datestring).getTime();
			} catch (ParseException e) {
				e.printStackTrace();
				return 0;
			}
		}
}

资源文件

MysqlDriver=com.mysql.jdbc.Driver
MysqlUrl=jdbc:mysql://localhost:3306/jdbc\u7684\u4F7F\u7528
MysqlUser=root
MysqlPassworld=root

对应表的类

package jdbc;

public class Demo_1 {//类名在与表名一致前要先符合书写规范
	private Integer id;
	private String name;
	private double money;
	private Integer cno;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	public Integer getCno() {
		return cno;
	}
	public void setCno(Integer cno) {
		this.cno = cno;
	}
	//构造方法只要求有一个空的构造方法,至于其他看具体使用情况创建即可
	public Demo_1(Integer id, String name, double money, Integer cno) {
		super();
		this.id = id;
		this.name = name;
		this.money = money;
		this.cno = cno;
	}
	public Demo_1(String name, double money, Integer cno) {
		super();
		this.name = name;
		this.money = money;
		this.cno = cno;
	}
	public Demo_1() {
		super();
	}
	
	

}

package jdbc;

public class Demo_2 {
	private Integer id;
	private String name;
	private String adress;
	private Integer cno;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAdress() {
		return adress;
	}
	public void setAdress(String adress) {
		this.adress = adress;
	}
	public Integer getCno() {
		return cno;
	}
	public void setCno(Integer cno) {
		this.cno = cno;
	}
	public Demo_2(Integer id, String name, String adress, Integer cno) {
		super();
		this.id = id;
		this.name = name;
		this.adress = adress;
		this.cno = cno;
	}
	public Demo_2(String name, String adress, Integer cno) {
		super();
		this.name = name;
		this.adress = adress;
		this.cno = cno;
	}
	public Demo_2() {
		super();
	}
	
}

上一篇:201521123050《Java程序设计》第2周学习总结


下一篇:Java面试题-数据库篇十二