日期:2014-05-19 浏览次数:20792 次
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
public final static String driver = "oracle.jdbc.driver.OracleDriver";
public final static String url = "jdbc:oracle:thin:@192.168.1.250:1521:YIDEEORC102010";
public final static String dbName = "estock";
public final static String dbPass = "estock";
private Connection conn = null;
public Connection getConn() throws ClassNotFoundException, SQLException
{
if(conn == null)
{
Class.forName(driver);
conn = DriverManager.getConnection(url,dbName,dbPass);
}
return conn;
}
public void closeAll(Connection conn,PreparedStatement pstmt,ResultSet rs)
{
if(rs != null)
{
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pstmt != null)
{
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null)
{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int executeSQL(String preparedSql,String[] param)
{
Connection conn = null;
PreparedStatement pstmt = null;
int num = 0;
try {
conn = getConn();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
pstmt = conn.prepareStatement(preparedSql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(param != null)
{
for(int i=1;i<param.length;i++)
{
try {
pstmt.setString(i+1, param[i]);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
num = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
closeAll(conn,pstmt,null);
return num;
}
}
package test;
import java.sql.Connection