在JSP中訪問數據庫大全
2024-09-05 00:19:04
供稿:網友
一、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連接access數據庫
<%@page import="java.sql.*"
import ="java.util.*"
import ="java.io.*"
import="java.text.*"
contenttype="text/html; charset=gb2312"
buffer="20kb"
%><%! int all,i,m_count;
string odbcquery;
connection odbcconn;
statement odbcstmt;
resultset odbcrs;
string username,title,content,work,email,url,time,date;
string datetime;
%>
<%
try{
class.forname("sun.jdbc.odbc.jdbcodbcdriver");
}catch (classnotfoundexception e)
{ out.print ("驅動程序不存在");
}
try{
odbcconn = drivermanager.getconnection("jdbc:odbc:db1");
odbcstmt = odbcconn.createstatement();
odbcquery="select * from book where datetime>2001-4-26 order by datetime desc";
odbcrs=odbcstmt.executequery(odbcquery);
int i=0;
while (i<130) odbcrs.next();
while (odbcrs.next())
{
//*/////////////////////////顯示數據庫的內容用于調試程序是用//
int ii;
try{
try{
for (ii=1;;ii++)
out.print ("<br>cloumn "+ii+" is: "+odbcrs.getstring(ii));
}catch (nullpointerexception e) {
out.print ("有空的指針");
}
}catch (sqlexception e){
}
}
odbcrs.close();
odbcstmt.close();
odbcconn.close();
}catch (sqlexception e)
{ out.print (e);
}
%>