如何封装查询记录到Java对象数组
来源:互联网 发布:js 滚动时间选择插件 编辑:程序博客网 时间:2024/06/11 10:45
如何封装查询记录到Java对象数组
1:通过Vector的toArray()方法:
举例如下:
(1)MySQL数据库中创建一个职位表,其中只包含两个字段,职位ID,职位名称, 建表语句如下:
CREATE TABLE POST (
POST_CODE int PRIMARY KEY ,
POST_NAME varchar(50) UNIQUE NOT NULL
) ;
(2)
插入几条测试记录:略。
(3)
创建VO:
package org.mixih.db;
public class PostRec {
private int postCode ;
private String postName ;
public PostRec() {
}
public PostRec(int PostCode ,String postName){
this.postCode = postCode ;
this.postName = postName ;
}
}
public int getPostCode() {
return postCode;
}
public void setPostCode(int postCode) {
this.postCode = postCode;
}
public String getPostName() {
return postName;
}
public void setPostName(String postName) {
this.postName = postName;
}
}
(4)创建数据库访问类:
package org.mixih.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
public class OprPost {
String error;
Connection con;
public OprPost() {
}
public void connect() throws ClassNotFoundException, SQLException,
Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("JDBC driver loaded");
con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/hrsys", "test", "test");
System.out.println("Database Connection established");
} catch (ClassNotFoundException e) {
error = "Cound not locate DB driver";
throw new ClassNotFoundException(error);
} catch (SQLException e) {
error = "SQLException : cound not connect to database.";
throw new SQLException(error);
} catch (Exception e) {
error = "Exception : an error occurred while connecting to database ";
throw new Exception(error);
}
}
public void disConnect() throws SQLException {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
error = "SQLExcepion : Unable to close the database connection.";
throw new SQLException(error);
}
}
public PostRec[] viewPost() throws SQLException, Exception {
ResultSet rs = null;
Vector vForTemp = new Vector();
try {
String queryString = "select * from post ;";
Statement stmt = con.createStatement();
rs = stmt.executeQuery(queryString);
while(rs.next()){
int postCode = rs.getInt(1);
String postName = rs.getString(2);
PostRec postRec = new PostRec(postCode ,
postName );
vForTemp.add(postRec) ;
}
} catch (SQLException e) {
error = " An exception occurred execute query .";
throw new SQLException(error);
} catch (Exception e) {
error = "Aneception occurred while while retriving books.";
throw new Exception(error);
}
return (PostRec[])vForTemp.toArray( new PostRec[0]);
}
public static void main(String[] args) throws Exception {
OprPost myOprPost = new OprPost();
myOprPost.connect();
PostRec[] postRecs = myOprPost.viewPost();
for(int i = 0 ; i < postRecs.length ; i++ ) {
System.out.println(postRecs[i].getPostCode());
System.out.println(postRecs[i].getPostName());
}
myOprPost.disConnect();
}
}
- 如何封装查询记录到Java对象数组
- SQL查询封装到对象
- Java记录 -9- 面向对象之封装
- Java记录 -33- 数组存放对象地址
- Java记录 -10- 面向对象之封装续
- Java记录 -11- 面向对象之封装续II
- 【IOS】如何封装结构体对象到NSMutableArray中
- vb如何将查询到的记录导出为Excel
- JSON对象数组到Java的转换
- SpringMVC集成Hibernate分页查询到对象数组问题
- 对象数组如何set值并加到对象里面
- java 工具类 ---反射字段值封装到对象中
- java 工具类 ---反射字段值封装到对象中
- 利用java反射将map值封装到对象中
- 在参数封装对象Page中使用in查询多项数据记录
- java封装sql查询
- java封装sql查询
- Java 封装jdbc查询
- 用 spring 实现观察者设计模式
- 正则表达式之全部符号解释
- CISCO命令全集
- 密码破解速度全面披露
- PLL
- 如何封装查询记录到Java对象数组
- PRO/ENGINEER wildfire 的安装
- IE用户请当心 微软四月3个漏洞已出现恶意攻击
- 关于MIDP 1.0 图片翻转换色的讨论
- 你是否注意到細節-1
- 由String 转为XX类型
- Java的中文化
- 4月1日愚人节,将堕落进行到底 之 skylin 版本(转移自MSN spaces)
- jsp转向