当前位置: 移动技术网 > IT编程>开发语言>Java > 在JSP中访问数据库大全

在JSP中访问数据库大全

2017年12月12日  | 移动技术网IT编程  | 我要评论
现在有好多初学jsp的网友经常会问数据库怎么连接啊,怎么老出错啊?所以我集中的在这写篇文章供大家参考,其实这

现在有好多初学jsp的网友经常会问数据库怎么连接啊,怎么老出错啊?所以我集中的在这写篇文章供大家参考,其实这种把数据库逻辑全部放在jsp里未必是好的做法,但是有利于初学者学习,所以我就这样做了,当大家学到一定程度的时候,可以考虑用mvc的模式开发。在练习这些代码的时候,你一定将jdbc的驱动程序放到服务器的类路径里,然后要在数据库里建一个表test,有两个字段比如为test1,test2,可以用下面sql建 create table test(test1 varchar(20),test2 varchar(20),然后向这个表写入一条测试纪录,那么现在开始我们的jsp和数据库之旅吧。 



一、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); 

%> 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网