當前位置:首頁 » 操作系統 » java封裝資料庫操作

java封裝資料庫操作

發布時間: 2022-09-18 08:01:15

⑴ 在java中將資料庫裡面的數據r如何封裝到一個集合裡面,菜鳥求大神指點迷津(集合的元素是對象 )

	while(rs.next()){
Subjectsubject=newSubject();
subject.setSubjectID(rs.getString("subjectID"));
subject.setSubjectTitle(rs.getString("subjectTitle"));
subject.setSubjectOptionA(rs.getString("subjectOptionA"));
subject.setSubjectOptionB(rs.getString("subjectOptionB"));
subject.setSubjectOptionC(rs.getString("subjectOptionC"));
subject.setSubjectOptionD(rs.getString("subjectOptionD"));
subject.setSubjectAnswer(rs.getString("subjectAnswer"));
subject.setSubjectParse(rs.getString("subjectParse"));
subjectList.add(subject);
}

這樣就可以了。你應該能明白吧。要每回new一個新的。

⑵ java 如何封裝一段程序 包含四個類,實現從資料庫取數據後操作

抽取服務介面,和與資料庫直接通信的DAO介面,然後提供這兩個介面的實現。
外部應用訪問服務介面,服務介面的實現類訪問DAO介面。。。封裝完了

⑶ jsp中封裝資料庫操作,例如資料庫連接,條件查詢等到javabean中,怎麼做

DBConnectionManager.java

import java.sql.*;
public class DBConnectionManager {
private String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=demo";
private String user="sa";
private String password="";
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public Connection getConnection(){
try{
Class.forName(driverName);
return DriverManager.getConnection(url, user, password);
}catch(Exception e){
e.printStackTrace();
return null;
}
}

}

DBSQLManager.java

import java.sql.*;
public class DBSQLManager {
protected Connection con=null;//Connection對象
protected Statement stmt=null;//Statement對象
protected ResultSet rs=null;//記錄結果集
protected String sql=""; //SQL語句

public DBSQLManager(){
try {
DBConnectionManager dcm=new DBConnectionManager();
con=dcm.getConnection();
//con.setAutoCommit(false);//添加事物,既是否自動提交
stmt=con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Statement getStmt(){
return stmt;
}
public Connection getCon(){
return con;
}
public ResultSet getRs(){
return rs;
}
public void setSql(String sql){
this.sql=sql;
}
public String getSql(){
return sql;
}
//查找
public void execueQuery(){
try {
rs=stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
//更新
public void executeUpdate(){
try {
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
//關閉
public void close(){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs=null;
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt=null;
}
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con=null;
}

}

如果樓主有不明白的,,網路HI我

祝樓主早日成功!

⑷ java:」將資料庫操作封裝成介面「是什麼意思要是封裝成類我明白是全裝在類里的意思,可介面不是沒

就是定義介面,完你的類去實現介面

之後在外面調用的時候是通過介面new實現類去調用鏈接。

這是java中的一種多態表現,是想讓你實現這個。。

⑸ java如何訪問資料庫

Java可以使用JDBC訪問資料庫,也可以使用各類ORM框架訪問資料庫,但這些框架最終還是通過JDBC訪問資料庫,它們只是封裝了資料庫操作,而使得開發者可以減少這部分消耗。因此,本文只講解JDBC訪問方式。
JDBC訪問一般分為如下流程:
1、載入JDBC驅動程序:
在連接資料庫之前,首先要載入想要連接的資料庫的驅動到JVM(Java虛擬機),
這通過java.lang.Class類的靜態方法forName(String className)實現。
例如:
try{
//載入MySql的驅動類
Class.forName("com.mysql.jdbc.Driver") ;
}catch(ClassNotFoundException e){
System.out.println("找不到驅動程序類 ,載入驅動失敗!");
e.printStackTrace() ;
}
成功載入後,會將Driver類的實例注冊到DriverManager類中。
2、提供JDBC連接的URL
•連接URL定義了連接資料庫時的協議、子協議、數據源標識。
•書寫形式:協議:子協議:數據源標識
協議:在JDBC中總是以jdbc開始
子協議:是橋連接的驅動程序或是資料庫管理系統名稱。
數據源標識:標記找到資料庫來源的地址與連接埠。
例如:(MySql的連接URL)
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk ;
useUnicode=true:表示使用Unicode字元集。如果characterEncoding設置為
gb2312或GBK,本參數必須設置為true 。characterEncoding=gbk:字元編碼方式。
3、創建資料庫的連接
•要連接資料庫,需要向java.sql.DriverManager請求並獲得Connection對象,該對象就代表一個資料庫的連接。
•使用DriverManager的getConnectin(String url,String username,String password )方法傳入指定的欲連接的資料庫的路徑、資料庫的用戶名和密碼來獲得。
例如:
//連接MySql資料庫,用戶名和密碼都是root
String url = "jdbc:mysql://localhost:3306/test" ;
String username = "root" ;
String password = "root" ;
try{
Connection con =
DriverManager.getConnection(url , username , password ) ;
}catch(SQLException se){
System.out.println("資料庫連接失敗!");
se.printStackTrace() ;
}
4、創建一個Statement
•要執行SQL語句,必須獲得java.sql.Statement實例,Statement實例分為以下3種類型:
1、執行靜態SQL語句。通常通過Statement實例實現。
2、執行動態SQL語句。通常通過PreparedStatement實例實現。
3、執行資料庫存儲過程。通常通過CallableStatement實例實現。
具體的實現方式:
Statement stmt = con.createStatement() ;
PreparedStatement pstmt = con.prepareStatement(sql) ;
CallableStatement cstmt = con.prepareCall("{CALL demoSp(? , ?)}") ;
5、執行SQL語句
Statement介面提供了三種執行SQL語句的方法:executeQuery 、executeUpdate和execute
1、ResultSet executeQuery(String sqlString):執行查詢資料庫的SQL語句,返回一個結果集(ResultSet)對象。
2、int executeUpdate(String sqlString):用於執行INSERT、UPDATE或DELETE語句以及SQL DDL語句,如:CREATE TABLE和DROP TABLE等
3、execute(sqlString):用於執行返回多個結果集、多個更新計數或二者組合的語句。
具體實現的代碼:
ResultSet rs = stmt.executeQuery("SELECT * FROM ...") ;
int rows = stmt.executeUpdate("INSERT INTO ...") ;
boolean flag = stmt.execute(String sql) ;
6、處理結果
兩種情況:
1、執行更新返回的是本次操作影響到的記錄數。
2、執行查詢返回的結果是一個ResultSet對象。
•ResultSet包含符合SQL語句中條件的所有行,並且它通過一套get方法提供了對這些行中數據的訪問。
•使用結果集(ResultSet)對象的訪問方法獲取數據:
while(rs.next()){
String name = rs.getString("name") ;
String pass = rs.getString(1); // 此方法比較高效(列是從左到右編號的,並且從列1開始)
}
7、關閉JDBC對象
操作完成以後要把所有使用的JDBC對象全都關閉,以釋放JDBC資源,關閉順序和聲明順序相反:
1、關閉記錄集
2、關閉聲明
3、關閉連接對象
if(rs != null){ // 關閉記錄集
try{
rs.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
if(stmt != null){ // 關閉聲明
try{
stmt.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
if(conn != null){ // 關閉連接對象
try{
conn.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}

⑹ java資料庫封裝

package com.lc.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Datebase{
public Connection conn=null;
public Statement stmt=null;
public ResultSet rs=null;
public PreparedStatement preparedstmt=null;
private static String dbDriver="com.mysql.jdbc.Driver";
private static String dbUrl="jdbc:mysql://localhost/graatedmanager?useUnicode=true&characterEncoding=gb2312";
private static String dbUser="root";
private static String dbPwd="root";
//打開資料庫連接
public Datebase(){}

public static Connection getConnection()
{
java.sql.Connection conn=null;
try
{
Class.forName(dbDriver);
conn=DriverManager.getConnection(dbUrl,dbUser,dbPwd);
}
catch(Exception e)
{
e.printStackTrace();
}
if(conn==null)
{
System.err.println("警告:資料庫連接失敗!");
}
return conn;

}

//讀取結果集
public ResultSet doQuery(String sql)
{
try
{
conn=Datebase.getConnection();
stmt=((java.sql.Connection) conn).createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}
catch(SQLException e)
{
e.printStackTrace();
}
return rs;
}

//更新數據
public int Prepared_doUpdate(String sql,String parameters[])
{
int result=0;
try
{
conn=Datebase.getConnection();
preparedstmt=conn.prepareStatement(sql);
/* for(int i=0;i<parameters.length;i++)
{
System.out.println(parameters[i]);
}*/
if(parameters!=null)
{
for(int i=0;i<parameters.length;i++)
{
preparedstmt.setString(i+1,parameters[i]);
}
}
result=preparedstmt.executeUpdate();
}
catch(SQLException e)
{
e.printStackTrace();
System.out.println("操作失敗");
result=0;
}
return result;

}

//更新數據
public int doUpdate(String sql)
{
int result=0;
try
{
conn=Datebase.getConnection();
stmt=((java.sql.Connection) conn).createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result=stmt.executeUpdate(sql);
}
catch(SQLException e)
{
result=0;
}
return result;
}

//關閉資料庫連接
public void closeConnection()
{
try
{
if(rs!=null)
rs.close();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
if(conn!=null)
((Statement) conn).close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

⑺ (JAVA)怎樣將對資料庫的增刪查改方法封裝起來便於以後調用

public class DBRecordSet {
static private Logger log = Logger.getLogger(DBRecordSet.class.getName());
ResultSetMetaData md = null;
Connection conInner = null;
private int firstElementOfThisList = 0; //當前緩沖池中保存的記錄在整個結果集中的位置
private int countOfElementsInthisList = 0; //緩沖池中記錄的數目
private List resultList = null; //保存結果的緩沖池
private Vector columnMap = null;
private int cacheSize = -1; //保存結果的緩沖池大小
private int maxRecords = 10; //執行結果集的時候得到的最多的記錄數

private String strSQLStmt = null; //打開結果集的時候執行的SQL語句
private boolean isClosed = true; //結果集是否已經open
private int columnCount = -1; //結果集欄位數目
private int columnTypeInt[] = null;
private String columnTypeString[] = null;

private int curRow = 0; // 當前游標所在行,基數為 1
private int maxRow = -1; // 執行查詢語句得到的記錄數,基數為 1
private int curPage = -1; // 分頁顯示時當前所在頁,基數為 1
private int pageSize = -1; // 分頁顯示時每頁記錄數,基數為 1
private int pageCount = -1; // 分頁顯示時總頁數,基數為 1
private int updateCount = -1;
private boolean cursorDir = true;

DBConnectionManager connMgr = null;

private DBConnectionManager getConnectionManager() {
if (connMgr == null) {
connMgr = DBConnectionManager.getInstance();
}

return connMgr;
}

private int getCacheSize() {
if (this.cacheSize == -1) {
cacheSize = getConnectionManager().getCacheSize();
if (cacheSize <= 0)
cacheSize = 50;
}
return this.cacheSize;
}

public void setCacheSize(int size) {
this.cacheSize = size;
}

/**
* 構造函數
*/
public DBRecordSet() {
close();
}

public int execute(Connection con, String sql) {
if (con == null || sql == null || sql.length() <= 0) {
return -1;
}

Statement stmt = null;

try {

if (con.isClosed()) return -1;

stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
// 設置 ResultSet 對象可包含的最多行數
if (maxRecords > 1) stmt.setMaxRows(maxRecords);
//執行語句,判斷語句類型
log.debug("開始執行SQL語句: " + sql);

int resultCount = stmt.executeUpdate(sql);

log.debug("執行SQL語句成功: " + sql + "; 返回結果數目為" + resultCount);

return resultCount;
} catch (Exception e) {
log.error("執行SQL語句失敗:" + e.getMessage());
} finally {
try {
if (stmt != null)
stmt.close();
} catch (Exception e) {
log.error("關閉Statement失敗:" + e.getMessage());
return -1;
}
}

return -1;
}

public boolean openSelect(Connection con, String sql) {
Statement stmt = null;
ResultSet rs = null;
int n = 0, i = 0;

if (con == null) return false;

firstElementOfThisList = 0;
countOfElementsInthisList = 0;

try {
if (con.isClosed()) return false;
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
// 設置 ResultSet 對象可包含的最多行數
if (maxRecords > 1) stmt.setMaxRows(maxRecords);
//執行語句,判斷語句類型
log.debug("開始執行SQL語句: " + sql);
if (stmt.execute(sql)) {
log.debug("執行查詢語句成功");
rs = stmt.getResultSet();
md = rs.getMetaData();
columnCount = md.getColumnCount();
updateCount = -1;

if (resultList != null) {
resultList.clear();
resultList = null;
}

resultList = new ArrayList();

if (columnMap != null) {
columnMap = null;
}
columnMap = new Vector(columnCount);

for (n = 1; n <= columnCount; ++n) {
String columnName = md.getColumnName(n).trim();
columnName = columnName.toLowerCase();
if (columnName == null || columnName.length() <= 0) {
columnName = "vcolumn_" + String.valueOf(n);
}
if (columnMap.contains(columnName)) {
columnName += "_" + String.valueOf(n);
}
columnMap.add(columnName);
}
/*
for (n = 1; n <= columnCount; ++n){
log.debug("查詢語句結果集的列" + n + ":" + String.valueOf(columnMap.get(n - 1)));
}
*/
PropertyContainer property = null;
while (rs.next() && i < getCacheSize()) {
property = new PropertyContainerImpl();
for (n = 1; n <= columnCount; ++n) {
try {
property.addPropertyBy(String.valueOf(columnMap.get(n - 1)), rs.getObject(n));
} catch (Exception e) {
property.addPropertyBy(String.valueOf(columnMap.get(n - 1)), rs.getString(n));
}
}
resultList.add(property);
i++;
}

if (property != null)
log.debug("Open查詢的最後一條記錄是:" + property.valueToString());

if (i > 0) { //如果有記錄取出
firstElementOfThisList = 1;
countOfElementsInthisList = resultList.size();
maxRow = i;
if (i >= getCacheSize()) { //記錄沒有取完
//注意:為了兼容以前代碼,這里需要將結果集滾動到最後,用來獲得記錄數目
while (rs.next()) maxRow++;
maxRow++;
}
curRow = 0;
} else {//如果沒有記錄
firstElementOfThisList = 0;
countOfElementsInthisList = 0;
curRow = -1;
maxRow = 0;
log.debug("沒有記錄返回:" + sql);
}

log.debug("open: 讀取從第0條記錄開始的" + getCacheSize() + "條記錄, 返回記錄" + countOfElementsInthisList + "條。總記錄數為" + maxRow + "條");
} else {
// 執行更新語句後將查詢結果集關閉並清除各項信息
int updatecount = stmt.getUpdateCount();
close();
updateCount = updatecount;
log.debug("成功執行更新語句");
} //保存執行的SQL語句
strSQLStmt = sql;

} catch (SQLException e) {
log.error(e.toString() + " : 執行SQL語句時出錯:" + sql);
return false;
} catch (Exception e) {
log.error(e.toString() + " : 執行SQL語句時出錯:" + sql);
return false;
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
//getConnectionManager().freeConnectionInner(con);
} catch (Exception e) {
log.error(e.toString() + " : 結果集不能正確關閉");
//getConnectionManager().freeConnectionInner(con);
return false;
}
}
isClosed = false;
return true;
}

/**
* 執行SQL語句,可以為查詢或更新語句。
* 執行更新語句後調用 getUpdateCount() 取得所更新的記錄數
*
* @param sql 執行的SQL語句
*/
public boolean open(String sql) {
Statement stmt = null;
ResultSet rs = null;
int n = 0, i = 0;

Connection con = getConnectionManager().getConnectionInner();
if (con == null) return false;

firstElementOfThisList = 0;
countOfElementsInthisList = 0;

try {
if (con.isClosed()) return false;
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
// 設置 ResultSet 對象可包含的最多行數
if (maxRecords > 1) stmt.setMaxRows(maxRecords);
//執行語句,判斷語句類型
log.debug("開始執行SQL語句: " + sql);
if (stmt.execute(sql)) {
log.debug("執行查詢語句成功");
rs = stmt.getResultSet();
md = rs.getMetaData();
columnCount = md.getColumnCount();
updateCount = -1;

if (resultList != null) {
resultList.clear();
resultList = null;
}

resultList = new ArrayList();

if (columnMap != null) {
columnMap = null;
}
columnMap = new Vector(columnCount);

for (n = 1; n <= columnCount; ++n) {
String columnName = md.getColumnName(n).trim();
columnName = columnName.toLowerCase();
if (columnName == null || columnName.length() <= 0) {
columnName = "vcolumn_" + String.valueOf(n);
}
if (columnMap.contains(columnName)) {
columnName += "_" + String.valueOf(n);
}
columnMap.add(columnName);
}
/*
for (n = 1; n <= columnCount; ++n){
log.debug("查詢語句結果集的列" + n + ":" + String.valueOf(columnMap.get(n - 1)));
}
*/
PropertyContainer property = null;
while (rs.next() && i < getCacheSize()) {
property = new PropertyContainerImpl();
for (n = 1; n <= columnCount; ++n) {
try {
property.addPropertyBy(String.valueOf(columnMap.get(n - 1)), rs.getObject(n));
} catch (Exception e) {
property.addPropertyBy(String.valueOf(columnMap.get(n - 1)), rs.getString(n));
}
}
resultList.add(property);
i++;
}

if (property != null)
log.debug("Open查詢的最後一條記錄是:" + property.valueToString());

if (i > 0) { //如果有記錄取出
firstElementOfThisList = 1;
countOfElementsInthisList = resultList.size();
maxRow = i;
if (i >= getCacheSize()) { //記錄沒有取完
//注意:為了兼容以前代碼,這里需要將結果集滾動到最後,用來獲得記錄數目
while (rs.next()) maxRow++;
maxRow++;
}
curRow = 0;
} else {//如果沒有記錄
firstElementOfThisList = 0;
countOfElementsInthisList = 0;
curRow = -1;
maxRow = 0;
log.debug("沒有記錄返回:" + sql);
}

log.debug("open: 讀取從第0條記錄開始的" + getCacheSize() + "條記錄, 返回記錄" + countOfElementsInthisList + "條。總記錄數為" + maxRow + "條");
} else {
// 執行更新語句後將查詢結果集關閉並清除各項信息
int updatecount = stmt.getUpdateCount();
close();
updateCount = updatecount;
log.debug("成功執行更新語句");
} //保存執行的SQL語句
strSQLStmt = sql;

} catch (SQLException e) {
log.error(e.toString() + " : 執行SQL語句時出錯:" + sql);
return false;
} catch (Exception e) {
log.error(e.toString() + " : 執行SQL語句時出錯:" + sql);
return false;
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
getConnectionManager().freeConnectionInner(con);
} catch (Exception e) {
log.error(e.toString() + " : 結果集不能正確關閉");
getConnectionManager().freeConnectionInner(con);
return false;
}
}
isClosed = false;
return true;
}

/**
* 根據語句,得到從startIndex開始的count條記錄
*/
private void getResultAt(int start, int count) throws Exception {

if (isClosed) {
throw new Exception("還沒有打開結果集");
}

Statement stmt = null;
ResultSet rs = null;

Connection con = getConnectionManager().getConnectionInner();
int readStart = start;
int readCount = count;
if (con == null) {
log.error("無法獲得有效連接");
throw new Exception("getResultAt: 無法獲得有效連接");
}
try {
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(strSQLStmt);

if (resultList != null) {
resultList.clear();
resultList = null;
}

resultList = new ArrayList();

// skip initial rows as specified by the start parameter.
while (start-- > 1 && rs.next()) ;

//分別對每一個欄位,取出數值,放到BaseBusinessObject的PropertyContainer中
while (count-- > 0 && rs.next()) {
PropertyContainer property = new PropertyContainerImpl();
for (int n = 1; n <= columnCount; ++n) {
try {
property.addPropertyBy(String.valueOf(columnMap.get(n - 1)), rs.getObject(n));
} catch (Exception e) {
property.addPropertyBy(String.valueOf(columnMap.get(n - 1)), rs.getString(n));
}
}
resultList.add(property);
}

log.debug("getResultAt: 讀取從第" + readStart + "條記錄開始的" + readCount + "條記錄, 返回記錄" + resultList.size() + "條");
} catch (SQLException e) {
throw new Exception(e.toString());
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
getConnectionManager().freeConnectionInner(con);
} catch (Exception e) {
log.error(e.toString() + " : 結果集不能正確關閉");
getConnectionManager().freeConnectionInner(con);
}
}
}

/**
* 執行SQL語句,可以為查詢或更新語句。
* 執行更新語句後調用 getUpdateCount() 取得所更新的記錄數
*
* @param sql 執行的SQL語句
*/
public boolean openGBK(String sql) {
return open(Convert.toGBK(sql));
}

/**
* 返回執行查詢語句後表的列數
*/
public int getColumnCount() {
return columnCount;
}

/**
* 返回每列的類型,基數為 1
*
* @param schema 表模式名
* @param table 表名
*/
public int[] getColumnType(String schema, String table) {
Connection con = null;
ResultSet results = null;
List list = new ArrayList();

if (columnTypeInt == null) {

log.debug("getColumnType: getConnection");
con = getConnectionManager().getConnectionInner();
if (con == null) return null;
try {
DatabaseMetaData dmd;
dmd = con.getMetaData();
results = dmd.getColumns(null, null, table.toUpperCase(), null);
//
while (results.next()){
list.add(new Integer(results.getInt("DATA_TYPE")));
}
columnTypeInt = new int[list.size()];
for(int i = 0; i < list.size(); i++){
Integer type = (Integer)list.get(i);
columnTypeInt[i] = type.intValue();
}
} catch (Exception e) {
return null;
} finally {
try {
results.close();
getConnectionManager().freeConnectionInner(con);
} catch (Exception e) {
getConnectionManager().freeConnectionInner(con);
log.error(e.toString() + " : 結果集不能正確關閉");
return null;
}
}
}
return columnTypeInt;
}

/**
* 返回每列的名稱,基數為 1
*
* @param schema 表模式名
* @param table 表名
*/
public String[] getColumnName(String schema, String table) {
Connection con = null;
ResultSet results = null;
if (columnTypeString == null) {
log.debug("getColumnName: getConnection");
con = getConnectionManager().getConnectionInner();
if (con == null) return null;
try {
DatabaseMetaData dmd;
dmd = con.getMetaData();
results = dmd.getColumns(null, null, table.toUpperCase(), null);
//
int i = 1;
while (results.next()) i++;
columnTypeString = new String[i];
i = 1;
results.beforeFirst();
while (results.next()) columnTypeString[i++] = results.getString("COLUMN_NAME").trim();
} catch (Exception e) {
return null;
} finally {
try {
results.close();
getConnectionManager().freeConnectionInner(con);
} catch (Exception e) {
getConnectionManager().freeConnectionInner(con);
log.error(e.toString() + " : 結果集不能正確關閉");
return null;
}
}
}
return columnTypeString;
}

/**
* 返回執行更新語句後實際更新的記錄數
*/
public int getUpdateCount() {
return updateCount;
}

/**
* 設置查詢語句執行完後取得的最大記錄數
*
* @param maxrec 最大記錄數
*/
public void setMaxRecords(int maxrec) {
maxRecords = maxrec;
}

/**
* 返回查詢語句執行完後取得的最大記錄數
*/
public int getMaxRecords() {
return maxRecords;
}

/**
* 關閉查詢結果集並清除各項信息
*/
public void close() {
md = null;
firstElementOfThisList = 0; //當前緩沖池中保存的記錄在整個結果集中的位置
countOfElementsInthisList = 0; //緩沖池中記錄的數目

if (resultList != null) {
int size = resultList.size();

⑻ java封裝資料庫連接,一般是寫到servlet中還是javaBean中

一般是javaBean中 servlet只是處理業務方面的問題。
等項目變大的時候會有action 這是後封裝數據的問題一樣還是
交給javaBean 處理。這樣的數據業務一般只能在數據訪問層的。

⑼ java對資料庫操作的封裝是怎麼樣的

java本身對資料庫沒有封裝,對資料庫封裝好的有hibernate,ibatis(mybatis),hibernate封裝的比較徹底,基本操作不用自己寫SQL語句,ibatis的話還是要自己寫SQL語句,比較靈活.

⑽ java ee如何使用javabean對一個操作資料庫的項目進行封裝

封裝成一個工具類的話,增刪改用ParameterMetaData pmd = stmt.getParameterMetaData(); int count = pmd.getParameterCount(); count是sql語句中的問號個數,比如 "insert into user (name,age) values (?,?)" ,count的值就為2,然後把數據放到一個Object里傳給工具類。 查詢操作,寫個handler,把bean.class傳給handler,查詢資料庫得到數據rs,....ResultSetMetaData rsmd = rs.getMetaData();int count = rsmd.getColumnCount();String columnName = rsmd.getColumnName(i);Object columnValue = rs.getObject(i);columnName是欄位名稱,columnValue是欄位的值,用暴力反射把數據封到Object里,在外面強轉就行。數據源看你自己想用什麼了

熱點內容
linux配置vim編譯c 發布:2025-05-18 03:55:07 瀏覽:99
砸百鬼腳本 發布:2025-05-18 03:53:34 瀏覽:934
安卓手機如何拍視頻和蘋果一樣 發布:2025-05-18 03:40:47 瀏覽:727
為什麼安卓手機連不上蘋果7熱點 發布:2025-05-18 03:40:13 瀏覽:797
網卡訪問 發布:2025-05-18 03:35:04 瀏覽:504
接收和發送伺服器地址 發布:2025-05-18 03:33:48 瀏覽:366
ef資料庫查詢數據 發布:2025-05-18 03:29:36 瀏覽:668
百度雲下載文件夾 發布:2025-05-18 03:17:33 瀏覽:674
php雲開發 發布:2025-05-18 03:12:41 瀏覽:447
sql語句顯示表 發布:2025-05-18 03:12:30 瀏覽:690