📄 databaseaccess.java
字号:
package com.coshare.joyteam.util;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class DatabaseAccess
{
private Connection dbConnection = null;
private DataSource dataSource = null;
private Statement statement = null;
private ResultSet resultset = null;
public DatabaseAccess()
{
}
/*
* getDataSource()
*/
public DataSource getDataSource()
{
try
{
String dataSourceURL = "java:/joyteamDS";
Context context = new InitialContext();
dataSource = (DataSource) context.lookup(dataSourceURL);
}
catch (NamingException e)
{
dataSource = null;
e.printStackTrace();
}
return dataSource;
}
/*
* getConnection()
*/
public Connection getConnection()
{
this.getDataSource();
if (dataSource != null)
{
try
{
String username = "";
String password = "";
dbConnection = dataSource.getConnection();
}
catch (SQLException e)
{
dbConnection = null;
e.printStackTrace();
}
}
else
{
dbConnection = null;
}
return dbConnection;
}
/*
* openConnection()
*/
public Connection openConnection()
{
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String db_URL = "";
String username = "";
String password = "";
try
{
Class.forName(driverName).newInstance();
dbConnection = DriverManager.getConnection(db_URL, username, password);
}
catch (InstantiationException e)
{
dbConnection = null;
e.printStackTrace();
}
catch (IllegalAccessException e)
{
dbConnection = null;
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
dbConnection = null;
e.printStackTrace();
}
catch (SQLException e)
{
dbConnection = null;
e.printStackTrace();
}
return dbConnection;
}
/*
* closeConnection()
*/
public void closeConnection()
{
if (dbConnection != null)
{
try
{
dbConnection.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
dbConnection = null;
}
}
}
/*
* createStatement()
*/
public Statement createStatement()
{
if (dbConnection != null)
{
try
{
statement = dbConnection.createStatement();
}
catch (SQLException e)
{
statement = null;
e.printStackTrace();
}
}
else
{
statement = null;
}
return statement;
}
/*
* prepareCall(String insqlStr)
*/
public CallableStatement prepareCall(String insqlStr)
{
if (dbConnection != null)
{
try
{
statement = dbConnection.prepareCall("{call" + insqlStr + "}");
}
catch (SQLException e)
{
e.printStackTrace();
statement = null;
}
}
else
{
statement = null;
}
return (CallableStatement)statement;
}
/*
* prepareStatement(String insqlStr)
*/
public PreparedStatement prepareStatement(String insqlStr)
{
if (dbConnection != null)
{
try
{
statement = dbConnection.prepareStatement(insqlStr);
}
catch (SQLException e)
{
statement = null;
e.printStackTrace();
}
}
else
{
statement = null;
}
return (PreparedStatement)statement;
}
/*
* closeStatement()
*/
public void closeStatement()
{
if (statement != null)
{
try
{
statement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
statement = null;
}
}
}
/*
* sqlExecute(String insqlStr)
*/
public ResultSet sqlExecute(String insqlStr)
{
try
{
statement = dbConnection.createStatement();
resultset = statement.executeQuery(insqlStr);
}
catch (SQLException e)
{
resultset = null;
e.printStackTrace();
}
return resultset;
}
/*
* beginTransaction()
*/
public void beginTransaction()
{
if (dbConnection != null)
{
try
{
dbConnection.setAutoCommit(false);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
/*
* commitTransaction()
*/
public void commitTransaction()
{
if (dbConnection != null)
{
try
{
dbConnection.commit();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -