?? mysql.java
字號:
package com.jdon.simpleregister;
import java.sql.*;
import javax.sql.*;
import java.io.*;
import javax.naming.*;
/**
*
* <p>Title: </p>
* <p>Description:
* JDBC通用操作類
*
* 關(guān)于數(shù)據(jù)庫的主要操作有:獲取數(shù)據(jù)庫連接;數(shù)據(jù)庫查詢、插入、修改、刪除;
* 斷開數(shù)據(jù)庫連接。這些數(shù)據(jù)庫操作對于操作不同的數(shù)據(jù)表應(yīng)該說都是統(tǒng)一的,
* 因此,數(shù)據(jù)庫的JDBC操作是可以做成一個通用類,這樣就能達(dá)到重用目的。
*
* </p>
* <p>Copyright: Jdon.com Copyright (c) 2003</p>
* <p>Company: 上海解道計算機(jī)技術(shù)有限公司</p>
* @author banq
* @version 1.0
*/
public class Mysql {
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement prepstmt = null;
/**
* 以創(chuàng)建Statement 初始化Mysql
*/
public Mysql() {
try {
getDataSource();
stmt = conn.createStatement();
} catch (Exception e) {
System.err.println("Mysql init error: " + e);
}
}
private void getDataSource() {
try {
Context ctx = new InitialContext();
if (ctx == null)
throw new Exception("Boom - No Context");
DataSource ds =
(DataSource) ctx.lookup("java:comp/env/jdbc/userDB");
if (ds != null)
conn = ds.getConnection();
} catch (Exception e) {
System.err.println("getDataSource() error: " + e);
}
}
private void getDirectConn(){
try {
Class.forName(Constants.dbdriver).newInstance();
conn = DriverManager.getConnection(Constants.dburl);
} catch (Exception e) {
System.err.println("getDataSource() error: " + e);
}
}
/**
* 以創(chuàng)建PreparedStatement 初始化Mysql
*/
public Mysql(String sql) {
try {
getDataSource();
prepareStatement(sql);
} catch (Exception e) {
System.err.println("Mysql init error: " + e);
}
}
public Connection getConnection() {
return conn;
}
public void prepareStatement(String sql) throws SQLException {
prepstmt = conn.prepareStatement(sql);
}
public void setString(int index, String value) throws SQLException {
prepstmt.setString(index, value);
}
public void setInt(int index, int value) throws SQLException {
prepstmt.setInt(index, value);
}
public void setBoolean(int index, boolean value) throws SQLException {
prepstmt.setBoolean(index, value);
}
public void setDate(int index, Date value) throws SQLException {
prepstmt.setDate(index, value);
}
public void setLong(int index, long value) throws SQLException {
prepstmt.setLong(index, value);
}
public void setFloat(int index, float value) throws SQLException {
prepstmt.setFloat(index, value);
}
public void setBinaryStream(int index, InputStream in, int length) throws
SQLException {
prepstmt.setBinaryStream(index, in, length);
}
public void clearParameters() throws SQLException {
prepstmt.clearParameters();
}
public PreparedStatement getPreparedStatement() {
return prepstmt;
}
public Statement getStatement() {
return stmt;
}
/**
* 執(zhí)行Statement查詢語句
* @param sql
* @return
* @throws SQLException
*/
public ResultSet executeQuery(String sql) throws SQLException {
if (stmt != null) {
return stmt.executeQuery(sql);
} else
return null;
}
/**
* 執(zhí)行PreparedStatement查詢語句
* @return
* @throws SQLException
*/
public ResultSet executeQuery() throws SQLException {
if (prepstmt != null) {
return prepstmt.executeQuery();
} else
return null;
}
/**
* 執(zhí)行Statement更改語句
* @param sql
* @throws SQLException
*/
public void executeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
}
/**
* 執(zhí)行PreparedStatement更改語句
* @throws SQLException
*/
public void executeUpdate() throws SQLException {
if (prepstmt != null)
prepstmt.executeUpdate();
}
/**
* 關(guān)閉連接
*/
public void close() {
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (prepstmt != null) {
prepstmt.close();
prepstmt = null;
}
conn.close();
conn = null;
} catch (Exception e) {
System.err.println("Mysql close error: " + e);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -