思路:
1、创建实体类对象
2、实体类来接数据库数据
3、创建对象集合,例如ArrayList,来存储数据
4、使用迭代器或是直接用循环输出数据
例子:
package A;
/*
*
* 宠物实体类
*
*/
public class pet {
private int id;
private int master_id;
private String name;
private int type_id;
private int health;
private int love;
private String adopt_time;
private String status;
//封装
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getMaster_id() {
return master_id;
}
public void setMaster_id(int masterId) {
master_id = masterId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getType_id() {
return type_id;
}
public void setType_id(int typeId) {
type_id = typeId;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public String getAdopt_time() {
return adopt_time;
}
public void setAdopt_time(String adoptTime) {
adopt_time = adoptTime;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
package A;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class B {
public List show(){
String classname="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String user="epet";
String pwd="123";
//创建对象集合
List petlist=new ArrayList();
Connection con=null;
PreparedStatement pre=null;
ResultSet rs=null;
try {
//加载驱动
Class.forName(classname);
//创建连接
con=DriverManager.getConnection(url,user,pwd);
//编写Sql语句
String sql="select id,master_id,nvl(name,'无名'),type_id,health,love,To_char(adopt_time,'yyyy\"年\"mm\"月\"dd\"日\"')," +
"decode(status,1,'正常',2,'禁用') from pet";
pre=con.prepareStatement(sql);
pre=con.prepareStatement(sql);
//得到结果集
rs=pre.executeQuery();
System.out.println("id\tm_id\t姓名\tt_id\t健康值\t亲密度\t领养时间\t\t\t状态");
System.out.println("----------------------------------------------------------------------------");
//while遍历输出
while(rs.next()){
//实例化宠物对象
pet pet=new pet();
pet.setId(rs.getInt(1));
pet.setMaster_id(rs.getInt(2));
pet.setName(rs.getString(3));
pet.setType_id(rs.getInt(4));
pet.setHealth(rs.getInt(5));
pet.setLove(rs.getInt(6));
pet.setAdopt_time(rs.getString(7));
pet.setStatus(rs.getString(8));
//把输出的每一个对象存到对象集合中
petlist.add(pet);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//释放内存
try {
if(rs !=null){
rs.close();
}
if(pre !=null){
pre.close();
}
if(con !=null){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return petlist;
}
}
package A;
import java.util.Iterator;
import java.util.List;
public class B_test {
public static void main(String[] args) {
//实例化B对象
B b=new B();
List petlist=b.show();
//用迭代器输出
Iterator it=petlist.iterator();
//迭代器使用while循环输出
while(it.hasNext()){
pet pet=it.next();
//打印输出
System.out.println(pet.getId()+"\t"+pet.getMaster_id()+"\t"+pet.getName()+"\t"+pet.getType_id()
+"\t"+pet.getHealth()+"\t"+pet.getLove()+"\t"+pet.getAdopt_time()+"\t"+pet.getStatus());
}
}
}