jsp怎样连接Oracle数据库

jsp怎样连接Oracle数据库,第1张

jsp连接oracle数据库,主要是通过jdbc技术,首先加载驱动类,然后连接url,输入用户名和密码,打开数据库连接资源,如下代码:

<%@page contentType="text/htmlcharset=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>

JSP连接Oracle10g数据库的方法:

<%@ page contentType="text/htmlcharset=gb2312"%>

<%@ page import="java.sql.*" %>

<%@ page import="java.io.*" %>

<%@ page import="java.util.*" %>

<html>

<head>

<title>Oracle数据库连接测试</title>

</head>

<body>

<%

java.sql.Connection lConn = null

java.sql.Statement lStat = null

java.sql.ResultSet lRs = null

try

{

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver())

String lUrl = "java:oracle:thin:@localhost:1521:orcl"

//java:oracle:thin: 表示使用的是thin驱动

//@srv:1521: 表示使用的服务器的名字和端口号

//dbname: 表示数据库的SID

lConn = DriverManager.getConnection(lUrl,"system","rg")

lStat = lConn.createStatement()

//创建表

String createTableCoffees = "CREATE TABLE COFFEES " +

"(COF_NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, " +

"SALES INTEGER, TOTAL INTEGER)"

lStat.executeUpdate(createTableCoffees)

//插入数据

lStat.executeUpdate("INSERT INTO COFFEES VALUES ('Colombian', 101, 7.99, 0, 0)")

lStat.executeUpdate("INSERT INTO COFFEES VALUES ('Espresso', 150, 9.99, 0, 0)")

lStat.executeUpdate("INSERT INTO COFFEES VALUES ('Colombian_Decaf', 101, 8.99, 0, 0)")

lStat.executeUpdate("INSERT INTO COFFEES VALUES ('French_Roast_Decaf', 49, 9.99, 0, 0)")

//查询结果

lRs = lStat.executeQuery("select * from COFFEES")

//显示结果

out.println("<table>")

while (lRs.next()) {

out.print("<tr><td>" + lRs.getString(1))

//COF_NAME

out.print( "<td>" + lRs.getInt(2))

//SUP_ID

out.print( "<td>" + lRs.getFloat(3))

//PRICE

out.print( "<td>" + lRs.getInt(4))

//SALES

out.println( "<td>" + lRs.getInt(5))

//TOTAL

}

out.println("</table>")

lRs.close()

lStat.close()

} catch (SQLException e) {

throw new ServletException(e)

} finally {

try {

if (lConn != null)

lConn.close()

} catch (SQLException e) {

}

}

%>

</body>

</html>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6765570.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-28
下一篇 2023-03-28

发表评论

登录后才能评论

评论列表(0条)

保存