當前位置:首頁 » 操作系統 » jsp連接oracle資料庫

jsp連接oracle資料庫

發布時間: 2022-12-19 05:53:21

⑴ 求高手!jsp訪問oracle資料庫的問題, 測試了好多次state.executeUpdate(sql);有問題,其它的都能通過。

你調試下,看看具體的異常到底是什麼。有了異常信息我們才可以知道到底是sql的問題還是資料庫連接的問題。不過根據你的描述,應該是sql的問題。

再一個問題。這個代碼應該是直接輸出異常信息不要輸出資料庫有問題。這樣不好判斷問題。

⑵ 在jsp中如何用連接池連接oracle資料庫

Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/sample");
Connection con = ds.getConnection();

⑶ 使用jsp連接oracle時,rs.next()值始終為false,表中存在數據

java">out.print(rs.next());

注意這句,因為你在這里調用過rs.next(),你已經取過一次了,所以下次再用時,就取不到了。把這句去掉。

如果去掉這句還是取不到數據,那就做如下檢查了:

  1. 資料庫鏈接是否成功?

  2. 你的sql是不是正確?

如果你是用eclipse開發的話,建議你下個斷點,這樣方便調試。

另外,你的代碼還有一個問題,資料庫連接conn 建議放在finally中關閉,同時結果集rs也要關閉。代碼如下:

try{
//你的代碼
}catch(SQLExceptionex){

}finally{
rs.close();
stmt.close();
conn.close();
}

⑷ 請問在JSP中如何用SELECT

一、jsp連接Oracle8/8i/9i資料庫(用thin模式)
testoracle.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl為你的資料庫的SID
String user="scott";
String password="tiger";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一個欄位內容為:<%=rs.getString(1)%>
您的第二個欄位內容為:<%=rs.getString(2)%>
<%}%>
<%out.print("資料庫操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
二、jsp連接Sql Server7.0/2000資料庫
testsqlserver.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//pubs為你的資料庫的
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一個欄位內容為:<%=rs.getString(1)%>
您的第二個欄位內容為:<%=rs.getString(2)%>
<%}%>
<%out.print("資料庫操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
三、jsp連接DB2資料庫
testdb2.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample";
//sample為你的資料庫名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一個欄位內容為:<%=rs.getString(1)%>
您的第二個欄位內容為:<%=rs.getString(2)%>
<%}%>
<%out.print("資料庫操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
四、jsp連接Informix資料庫
testinformix.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url =
"jdbc:informix-sqli://123.45.67.89:1533/testDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword";
//testDB為你的資料庫名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一個欄位內容為:<%=rs.getString(1)%>
您的第二個欄位內容為:<%=rs.getString(2)%>
<%}%>
<%out.print("資料庫操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
五、jsp連接Sybase資料庫
testmysql.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/tsdata";
//tsdata為你的資料庫名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一個欄位內容為:<%=rs.getString(1)%>
您的第二個欄位內容為:<%=rs.getString(2)%>
<%}%>
<%out.print("資料庫操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
六、jsp連接MySQL資料庫
testmysql.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//testDB為你的資料庫名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一個欄位內容為:<%=rs.getString(1)%>
您的第二個欄位內容為:<%=rs.getString(2)%>
<%}%>
<%out.print("資料庫操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
七、jsp連接PostgreSQL資料庫
testmysql.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/soft"
//soft為你的資料庫名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一個欄位內容為:<%=rs.getString(1)%>
您的第二個欄位內容為:<%=rs.getString(2)%>
<%}%>
<%out.print("資料庫操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>

⑸ jsp,用spring框架連接oracle資料庫

用spring框架連接oracle資料庫可以通過配置數據源的方式:
Spring 資料庫連接配置
oracle為例來說明:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.101)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=orcl)(SERVER=DEDICATED)))" />
<property name="username" value="rootl" />
<property name="password" value="1234" />
</bean>
擴展其他資料庫:
二 DB2
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.ibm.db2.jdbc.app.DB2Driver" />
<property name="url" value="jdbc:db2:thin:@localhost:5000/testDB" />
<property name="username" value="rootl" />
<property name="password" value="1234" />
</bean>
三 SQL Server
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
<property name="url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName = testDB" />
<property name="username" value="rootl" />
<property name="password" value="1234" />
</bean>
三 MySQL
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost/ testDB" />
<property name="username" value="rootl" />
<property name="password" value="1234" />
</bean>

org.apache.commons.dbcp.BasicDataSource 需要commons-pool.jar,commons-dbcp-1.2.2.jar,commons-collections-3.2.jar三個JAR包

⑹ jsp怎麼連接資料庫oracle

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>

⑺ jsp調用oracle資料庫存儲過程(追50分)

沒見過你這么調存儲過程的!
給你個參考,我自己寫的!

1.建一個程序包。如下:
create or replace package testpackage
is
type Test_Cursor is ref cursor;

Procere testc(
p_Cursor out testpackage.Test_Cursor
);
end testpackage;

2.建立存儲過程,存儲過程為:
create or replace package body testpackage
is
Procere testc(
p_Cursor out testpackage.Test_Cursor
)
As

Begin
open p_Cursor for select USER_NAME from bj.test;

end;

end testpackage;

JAVA代碼為:
public static void main(String[] args) {
Connection conn = null;
String url = "jdbc:oracle:thin:@172.16.0.139:1521:ORATEST";
try {
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = DriverManager.getConnection(url, "bj", "bj");
/*調用開始*/
CallableStatement cstmt = conn.prepareCall("{call bj.testpackage.TESTC(?)}");
cstmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
boolean bool = cstmt.execute();
/*調用結束*/
System.out.println(bool);
ResultSet rs = (ResultSet)cstmt.getObject(1);
while(rs.next()){
System.out.print(rs.getString("USER_ID") + " ");
System.out.print(rs.getString("USER_NAME") + " ");
System.out.println(rs.getString("USER_PWD"));
}
rs.close();
cstmt.close();
conn.close();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}

補充一下,要返回ResultSet需要用到oracle的package這個東西

熱點內容
購買雲伺服器並搭建自己網站 發布:2025-05-14 13:20:31 瀏覽:687
sqlserver建立視圖 發布:2025-05-14 13:11:56 瀏覽:484
搭建httpsgit伺服器搭建 發布:2025-05-14 13:09:47 瀏覽:255
新電腦拿回來我該怎麼配置 發布:2025-05-14 13:09:45 瀏覽:240
視頻伺服器新建ftp用戶 發布:2025-05-14 13:03:09 瀏覽:225
php花生 發布:2025-05-14 12:54:30 瀏覽:550
java人才 發布:2025-05-14 12:29:10 瀏覽:649
如何打開軟密碼 發布:2025-05-14 12:28:55 瀏覽:427
七牛存儲待遇 發布:2025-05-14 12:27:20 瀏覽:422
C語言a35a4a5 發布:2025-05-14 11:53:48 瀏覽:814