?? dbhelper.java
字號:
package com.mycompany.myapp.dao.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mycompany.myapp.util.Constants;
/**
* the database helper with some convenience methods for jdbc operations.
*/
public class DbHelper {
public static Connection conn;
/**
* get the connection
* @return
* @throws Exception
*/
public static Connection getConnection() throws Exception {
Class.forName(Constants.CLASS_NAME);
conn = DriverManager.getConnection(Constants.CONNET_STR,
Constants.USER, Constants.PASSWORD);
return conn;
}
/**
* get the resultSet
* @param sql
* @return
* @throws Exception
*/
public static ResultSet getResultSet(String sql) throws Exception {
boolean bSuccess = true;
ResultSet rs = null;
Statement stmt;
Connection con = getConnection();
if (con == null)
bSuccess = false;
if (bSuccess) {
try {
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
bSuccess = false;
}
}
if (bSuccess)
return rs;
else
return null;
}
/**
* execute the sql
* @param sql
* @return
* @throws Exception
*/
public static boolean execute(String sql) throws Exception {
boolean bSuccess = true;
Statement stmt = null;
Connection con = getConnection();
if (con == null)
bSuccess = false;
if (bSuccess) {
try {
stmt = con.createStatement();
bSuccess = stmt.execute(sql);
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
bSuccess = false;
}
}
return bSuccess;
}
/**
* close the connection
* @param conn
*/
public static void closeConnection() {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -