把数据库的数据在浏览器显示


思路:
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());
}


}

}

优质内容筛选与推荐>>
1、【转】easyui $.message.alert 点击右上角的关闭按钮时,不执行定义的回调函数
2、在Prism中利用Region的对ContentControl的自动充填问题
3、 c#.net常用的小函数和方法集
4、玩转X-CTR100 | X-Assistant串口助手控制功能
5、通过Flash Remoting,怎么通过页面主动传递值给Flash player呢?


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号