package s2jsp.flydream.pjsong.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="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //JDBC驱动程序,根据您使用的数据库来定,这里为SQL Server2008为例
public final static String URL="jdbc:sqlserver://localhost:1433;DataBaseName=myBlog"; //连接,也是根据您使用的数据库来定,这里为SQL Server2008为例
public final static String DBNAME="sa"; //数据库登录用户名
public final static String DBPASS="administrator315"; //数据库登录密码
public Connection getConn() throws ClassNotFoundException, SQLException{ //进行连接数据库
Class.forName(DRIVER);
Connection conn=DriverManager.getConnection(URL,DBNAME,DBPASS);
return conn;
}
public void closeAll(Connection conn,PreparedStatement pstmt,ResultSet res){ //关闭连接,释放结果集
if(res!=null){
try {
res.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
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();
pstmt=conn.prepareStatement(preparedSql);
if(param!=null){
for(int i=0;i<param.length;i++){
pstmt.setString(i+1, param[i]);
}
}
num=pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeAll(conn,pstmt,null);
}
return num;
}
}