在中访问数据库大全计划
中医诊断 2021年09月06日 浏览:3 次
现在有好多初学jsp的友经常会问数据库怎么连接啊,怎么老出错啊?所以我集中的在这写篇文章供大家参考,其实这种把数据库逻辑全部放在jsp里未必是好的做法,但是有利于初学者学习,所以我就这样做了,当大家学到一定程度的时候,可以考虑用MVC的模式开发。在练习这些代码的时候,你一定将jdbc的驱动程序放到服务器的类路径里,然后要在数据库里建一个表test,有两个字段比如为test1,test2,可以用下面SQL建 create table test(test1 varchar(20),test2 varchar(20),然后向这个表写入一条测试纪录,那么现在开始我们的jsp和数据库之旅吧。
一、jsp连接Oracle8/8i/9i数据库(用thin模式)
p如下:
<%@ page contentType=\"text/html;charset=gb2312\"%>
<%@ page import=\"l.*\"%>
<html>
<body>
<%rName(\"acleDriver\").newInstance();
String url=\"jdbc:oracle:thin:@localhost:1521:orcl\";
//orcl为你的数据库的SID
String user=\"scott\";
String password=\"tiger\";
Connection conn= tConnection(url,user,password);
Statement stmt=eateStatement(PE_SCROLL_SENSITIVE,NCUR_UPDATABLE);
String sql=\"select * from test\";
ResultSet rs=ecuteQuery(sql);
while(xt()) {%>
您的第一个字段内容为:<%=tString(1)%>
您的第二个字段内容为:<%=tString(2)%>
<%}%>
<%int(\"数据库操作成功,恭喜你\");%>
<%ose();
ose();
ose();
%>
</body>
</html> 二、jsp连接Sql Server7.0/2000数据库
p如下:
<%@ page contentType=\"text/html;charset=gb2312\"%>
<%@ page import=\"l.*\"%>
<html>
<body>
<%rName(\"LServerDriver\").newInstance();
String url=\"jdbc:microsoft:
//pubs为你的数据库的
String user=\"sa\";
String password=\"\";
Connection conn= tConnection(url,user,password);
Statement stmt=eateStatement(PE_SCROLL_SENSITIVE,NCUR_UPDATABLE);
String sql=\"select * from test\";
ResultSet rs=ecuteQuery(sql);
while(xt()) {%>
您的第一个字段内容为:<%=tString(1)%>
您的第二个字段内容为:<%=tString(2)%>
不只是简单线上的活动 <%}%>
<%int(\"数据库操作成功,恭喜你\");%>
<%ose();
ose();
ose();
%>
</body>
</html> 三、jsp连接DB2数据库
p如下:
<%@ page contentType=\"text/html;charset=gb2312\"%>
<%@ page import=\"l.*\"%>
<html>
<body>
<%rName(\"2Driver \").newInstance();
String url=\"jdbc:db2://localhost:5000/sample\";
//sample为你的数据库名
String user=\"admin\";
String password=\"\";
Connection conn= tConnection(url,user,password);
Statement stmt=eateStatement(PE_SCROLL_SENSITIVE,
NCUR_UPDATABLE);
String sql=\"select * from test\";
ResultSet rs=ecuteQuery(sql);
while(xt()) {%>
您的第一个字段内容为:<%=tString(1)%>
您的第二个字段内容为:<%=tString(2)%>
<%}%>
<%int(\"数据库操作成功,恭喜你\");%>
<%ose();
ose();
ose();
%>
</body>
</html> 四、jsp连接Informix数据库
p如下:
<%@ page contentType=\"text/html;charset=gb2312\"%>
<%@ page import=\"l.*\"%>
<html>
<body>
<%rName(\"xDriver\").newInstance();
String url =
\"jdbc:informix-
user=testuser;password=testpassword\";
//testDB为你的数据库名
Connection conn= tConnection(url);
Statement stmt=eateStatement(PE_SCROLL_SENSITIVE,NCUR_UPDATABLE);
String sql=\"select * from test\";
ResultSet rs=ecuteQuery(sql);
while(xt()) {%>
您的第一个字段内容为:<%=tString(1)%>
您的第二个字段内容为:<%=tString(2)%>
<%}%>
<%int(\"数据库操作成功,恭喜你\");%>
<%ose();
ose();
ose();
%>
</body>
</html> 五、jsp连接Access数据库
<%@page import=\"l.*\"
import =\"il.*\"
import =\".*\"
import=\"xt.*\"
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{
rName(\"bcOdbcDriver\");
}catch (ClassNotFoundException e)
{ int (\"驱动程序不存在\");
}
try{
odbcconn = tConnection(\"jdbc:odbc:db1\");
odbcstmt = eateStatement();
odbcQuery=\"Select * From book where datetime> Order By datetime DESC\";
odbcrs=ecuteQuery(odbcQuery);
int i=0;
while (i<130) xt();
while (xt())
{
//*/////////////////////////显示数据库的内容用于调试程序是用//
int ii;
try{
try{
for (ii=1;;ii++)
int (\"<br>Cloumn \"+ii+\" is: \"+tString(ii));
}catch (NullPointerException e) {
int (\"有空的指针\");
}
}catch (SQLException e){
}
}
ose();
ose();
ose();
}catch (SQLException e)
{ int (e);
}
%> 查看本文来源
达霏欣米诺地尔搽剂2%多久起效台州医院妇科哪家医院好
长沙哪医院治疗男科好

- 上一篇: 在中用连接各种数据库之物业
- 下一篇 在如何自定义错误网页覆盖
-
人参好坏差别大如何挑选
2019-07-16
-
刺玫的功效与作用
2019-07-07
-
白金古榄的功效与作用
2019-07-07
-
北沙参的功效与作用
2019-07-07
-
按摩赶走女性手脚冰凉
2019-07-05
-
棠梨枝叶的功效与作用
2019-07-02