jsp怎么连接数据库oracle
1个回答
2016-03-21 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
JSP连接Oracle10g数据库的方法:
<%@ page contentType="text/html;charset=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>
<%@ page contentType="text/html;charset=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>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询