struts:用struts框架的时候对数据库的操作,增删改查之后的写在哪呢?
是不是连接数据库写在一个java里,但是我在每个action里都要进行增删改查我怎么得到那个数据库连接呢?...
是不是连接数据库写在一个java里,但是我在每个action里都要进行增删改查我怎么得到那个数据库连接呢?
展开
展开全部
下边是一个jdbc 的
import java.sql.CallableStatement;
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 ConnectionPool {
//public static final String DATA_SOURCE = "java:comp/env/jdbc/orcl";
public static final String DATA_SOURCE = "java:comp/env/jdbc/orcl";
private ConnectionPool() {
}
/*
public static Connection getConnection() {
DataSource ds = null;
Connection conn = null;
try {
InitialContext jndiCntx = new InitialContext();
ds = (DataSource) jndiCntx.lookup(DATA_SOURCE);
conn = ds.getConnection();
}
catch (NamingException ne) {
LoggerUtils.recordError(ReportConstants.REPORT_SYS_LOG,
"getConnection NamingError: " + ne.toString());
}
catch (Exception e) {
LoggerUtils.recordError(ReportConstants.REPORT_SYS_LOG,
"getConnection Error: " + e.toString());
}
return conn;
}
*/
public static Connection getConnection() {
Connection retval = null;
String driverName = "oracle.jdbc.driver.OracleDriver";
String connectionString = "jdbc:oracle:thin:@192.168.1.1:1521:orcl";
String userName = "test";
String password = "test";
try {
Class.forName(driverName);
retval = DriverManager.getConnection(
connectionString, userName, password);
}
catch (ClassNotFoundException e) {
System.err.println("DB driver not fount");
}
catch (SQLException e) {
System.err.println("Can't connect to server");
}
return retval;
}
/**
* Close a connection.
* @param conn Pass the connection object to be closed.
*/
public static void releaseConnection(Connection conn){
if (conn != null) {
try {
conn.close();
}
catch (SQLException e) {
// LoggerUtils.recordError(ReportConstants.REPORT_SYS_LOG,
// "releaseConnection Error: " + e.toString());
}
}
}
/**
* Close a prepared statement.
* @param psmt Pass the prepared statements to be closed.
*/
public static void releasePreparedStatement(PreparedStatement psmt) {
if (psmt != null) {
try {
psmt.close();
}
catch (SQLException e) {
// LoggerUtils.recordError(ReportConstants.REPORT_SYS_LOG,
// "releasePreparedStatement Error: " + e.toString());
}
}
}
/**
* Close a statement.
* @param smt Pass the statement to be closed.
*/
public static void releaseStatement(Statement smt) {
if (smt != null) {
try {
smt.close();
}
catch (SQLException e) {
// LoggerUtils.recordError(ReportConstants.REPORT_SYS_LOG,
// "releaseStatement Error: " + e.toString());
}
}
}
/**
* Close a callable statement.
* @param cstmt Pass the callable statements to be closed.
*/
public static void releaseCallableStatement(CallableStatement cstmt) {
if (cstmt != null) {
try {
cstmt.close();
}
catch (SQLException e) {
/* LoggerUtils.recordError(ReportConstants.REPORT_SYS_LOG,
"releaseCallableStatement Error: " + e.toString());*/
}
}
}
/**
* Close a resultset
* @param rs Pass the resultset to be closed.
*/
public static void releaseResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
}
catch (SQLException e) {
/* LoggerUtils.recordError(ReportConstants.REPORT_SYS_LOG,
"releaseResultSet Error: " + e.toString());*/
}
}
}
public static void main(String arg[]){
System.out.println(ConnectionPool.getConnection());;
}
}
import java.sql.CallableStatement;
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 ConnectionPool {
//public static final String DATA_SOURCE = "java:comp/env/jdbc/orcl";
public static final String DATA_SOURCE = "java:comp/env/jdbc/orcl";
private ConnectionPool() {
}
/*
public static Connection getConnection() {
DataSource ds = null;
Connection conn = null;
try {
InitialContext jndiCntx = new InitialContext();
ds = (DataSource) jndiCntx.lookup(DATA_SOURCE);
conn = ds.getConnection();
}
catch (NamingException ne) {
LoggerUtils.recordError(ReportConstants.REPORT_SYS_LOG,
"getConnection NamingError: " + ne.toString());
}
catch (Exception e) {
LoggerUtils.recordError(ReportConstants.REPORT_SYS_LOG,
"getConnection Error: " + e.toString());
}
return conn;
}
*/
public static Connection getConnection() {
Connection retval = null;
String driverName = "oracle.jdbc.driver.OracleDriver";
String connectionString = "jdbc:oracle:thin:@192.168.1.1:1521:orcl";
String userName = "test";
String password = "test";
try {
Class.forName(driverName);
retval = DriverManager.getConnection(
connectionString, userName, password);
}
catch (ClassNotFoundException e) {
System.err.println("DB driver not fount");
}
catch (SQLException e) {
System.err.println("Can't connect to server");
}
return retval;
}
/**
* Close a connection.
* @param conn Pass the connection object to be closed.
*/
public static void releaseConnection(Connection conn){
if (conn != null) {
try {
conn.close();
}
catch (SQLException e) {
// LoggerUtils.recordError(ReportConstants.REPORT_SYS_LOG,
// "releaseConnection Error: " + e.toString());
}
}
}
/**
* Close a prepared statement.
* @param psmt Pass the prepared statements to be closed.
*/
public static void releasePreparedStatement(PreparedStatement psmt) {
if (psmt != null) {
try {
psmt.close();
}
catch (SQLException e) {
// LoggerUtils.recordError(ReportConstants.REPORT_SYS_LOG,
// "releasePreparedStatement Error: " + e.toString());
}
}
}
/**
* Close a statement.
* @param smt Pass the statement to be closed.
*/
public static void releaseStatement(Statement smt) {
if (smt != null) {
try {
smt.close();
}
catch (SQLException e) {
// LoggerUtils.recordError(ReportConstants.REPORT_SYS_LOG,
// "releaseStatement Error: " + e.toString());
}
}
}
/**
* Close a callable statement.
* @param cstmt Pass the callable statements to be closed.
*/
public static void releaseCallableStatement(CallableStatement cstmt) {
if (cstmt != null) {
try {
cstmt.close();
}
catch (SQLException e) {
/* LoggerUtils.recordError(ReportConstants.REPORT_SYS_LOG,
"releaseCallableStatement Error: " + e.toString());*/
}
}
}
/**
* Close a resultset
* @param rs Pass the resultset to be closed.
*/
public static void releaseResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
}
catch (SQLException e) {
/* LoggerUtils.recordError(ReportConstants.REPORT_SYS_LOG,
"releaseResultSet Error: " + e.toString());*/
}
}
}
public static void main(String arg[]){
System.out.println(ConnectionPool.getConnection());;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询