jsp怎样连接Oracle数据库
3个回答
展开全部
jsp连接oracle数据库,主要是通过jdbc技术,首先加载驱动类,然后连接url,输入用户名和密码,打开数据库连接资源,如下代码:
<%@page contentType="text/html;charset=gb2312"%>
<%@page import="java.sql.*"%>
<HTML><BODY>
<hr align="center" width="55%" color="#991111" size="5"/>
<%
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
request.setCharacterEncoding("GBK");
try{
Class.forName("oracle.jdbc.driver.OracleDriver");//加载oracle数据库驱动
}
catch(ClassNotFoundException ce){
out.println(ce.getMessage());
}
try{
String url="jdbc:oracle:thin:@localhost:1521:orcl";//连接oracle数据库的url
conn=DriverManager.getConnection(url,"scott","tiger");//开始连接
stmt=conn.prepareStatement("select * from emp");//获取返回的结果集
rs=stmt.executeQuery();
out.print("<TABLE BORDER='1' width='55%' cellpadding='3' align=center>");
out.print("<TR><TD>"+"<b>Empno</b>"+"</TD>");
out.print("<TD>"+"<b>Ename</b>"+"</TD>");
out.print("<TD>"+"<b>Job</b>"+"</TD>");
out.print("<TD>"+"<b>Mgr</b>"+"</TD>");
out.print("<TD>"+"<b>hiredate</b>"+"</TD>");
out.print("<TD align=center>"+"<b>Sal</b>"+"</TD>");
out.print("<TD>"+"<b>Comm</b>"+"</TD>");
out.print("<TD>"+"<b>Deptno</b>"+"</TD></TR>");
while(rs.next()){
out.print("<TR><TD>"+rs.getString(1)+"</TD>");
out.print("<TD>"+rs.getString(2)+"</TD>");
out.print("<TD >"+rs.getString(3)+"</TD>");
out.print("<TD>"+rs.getString(4)+"</TD>");
out.print("<TD>"+rs.getString(5)+"</TD>");
out.print("<TD>"+rs.getString(6)+"</TD>");
out.print("<TD>"+rs.getString(7)+"</TD>");
out.print("<TD>"+rs.getString(8)+"</TD></TR>");
}
out.print("</TABLE>");
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
%>
</BODY></HTML>
2014-03-04
展开全部
辅助类:
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 的,你改一下驱动!
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-03-04
展开全部
配置数据源,使用jdbc连
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询