jsp怎样连接Oracle数据库
2个回答
展开全部
辅助类:
package dao;
import java.util.*;
import java.sql.*;
public class BaseDao {
public static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=bbsMSG";
public static final String USER="sa";
public static final String PASS="sa";
public Connection connection=null;
public PreparedStatement preparedStatement=null;
public ResultSet resultSet=null;
//获得连接
public void getConnection(){
try {
Class.forName(DRIVER);//注册驱动
try {
connection=DriverManager.getConnection(URL, USER, PASS);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//关闭连接
public void closeAll(){
try {
if(resultSet!=null){resultSet.close();}
if(preparedStatement!=null){preparedStatement.close();}
if(connection!=null){connection.close();}
} catch (SQLException e) {
e.printStackTrace();
}
}
//执行SQL语句
public int executeSQL(String sql,String[] param){
int result=0;
getConnection();
try {
preparedStatement=connection.prepareStatement(sql);
if(param!=null){
for(int i=0;i<param.length;i++){
preparedStatement.setString(i+1, param[i]);
}
}
result=preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll();
}
return result;
}
}
只不过这是链接Sqlserver 的,你改一下驱动!
package dao;
import java.util.*;
import java.sql.*;
public class BaseDao {
public static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=bbsMSG";
public static final String USER="sa";
public static final String PASS="sa";
public Connection connection=null;
public PreparedStatement preparedStatement=null;
public ResultSet resultSet=null;
//获得连接
public void getConnection(){
try {
Class.forName(DRIVER);//注册驱动
try {
connection=DriverManager.getConnection(URL, USER, PASS);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//关闭连接
public void closeAll(){
try {
if(resultSet!=null){resultSet.close();}
if(preparedStatement!=null){preparedStatement.close();}
if(connection!=null){connection.close();}
} catch (SQLException e) {
e.printStackTrace();
}
}
//执行SQL语句
public int executeSQL(String sql,String[] param){
int result=0;
getConnection();
try {
preparedStatement=connection.prepareStatement(sql);
if(param!=null){
for(int i=0;i<param.length;i++){
preparedStatement.setString(i+1, param[i]);
}
}
result=preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeAll();
}
return result;
}
}
只不过这是链接Sqlserver 的,你改一下驱动!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询