最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > jsp教程

jsp编程:jsp连接数据库大全

日期:10-31    来源:中国设计秀    作者:cnwebshow.com

现在有好多初学jsp的网友经常会问数据库怎么连接啊,怎么老出错啊?所以我集中的在这写篇文章供大家参考,其实这种把数据库逻辑全部放在jsp里未必是好的做法,但是有利于初学者学习,所以我就这样做了,当大家学到一定程度的时候,可以考虑用MVC的模式开发。在练习这些代码的时候,你一定将jdbc的驱动程序放到服务器的类路径里,然后要在数据库里建一个表test,有两个字段比如为test1,test2,可以用下面SQL建 KJp中国设计秀
create table test(test1 varchar(20),test2 varchar(20) KJp中国设计秀
然后向这个表写入一条测试纪录 KJp中国设计秀
那么现在开始我们的jsp和数据库之旅吧。 KJp中国设计秀
一、jsp连接Oracle8/8i/9i数据库(用thin模式) KJp中国设计秀
  testoracle.jsp如下: KJp中国设计秀
<%@ page contentType="text/html;charset=gb2312"%>   KJp中国设计秀
<%@ page import="java.sql.*"%> KJp中国设计秀
<html>   KJp中国设计秀
<body>   KJp中国设计秀
<%Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();   KJp中国设计秀
String url="jdbc:oracle:thin:@localhost:1521:orcl"; KJp中国设计秀
//orcl为你的数据库的SID KJp中国设计秀
String user="scott"; KJp中国设计秀
String password="tiger"; KJp中国设计秀
Connection conn= DriverManager.getConnection(url,user,password);   KJp中国设计秀
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);   KJp中国设计秀
String sql="select * from test";   KJp中国设计秀
ResultSet rs=stmt.executeQuery(sql);   KJp中国设计秀
while(rs.next()) {%>   KJp中国设计秀
您的第一个字段内容为:<%=rs.getString(1)%>   KJp中国设计秀
您的第二个字段内容为:<%=rs.getString(2)%>   KJp中国设计秀
<%}%>   KJp中国设计秀
<%out.PRint("数据库操作成功,恭喜你");%>   KJp中国设计秀
<%rs.close();   KJp中国设计秀
stmt.close();   KJp中国设计秀
conn.close();   KJp中国设计秀
%>   KJp中国设计秀
</body>   KJp中国设计秀
</html> KJp中国设计秀
二、jsp连接Sql Server7.0/2000数据库 KJp中国设计秀
testsqlserver.jsp如下: KJp中国设计秀
<%@ page contentType="text/html;charset=gb2312"%>   KJp中国设计秀
<%@ page import="java.sql.*"%> KJp中国设计秀
<html>   KJp中国设计秀
<body>   KJp中国设计秀
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();   KJp中国设计秀
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"; KJp中国设计秀
//pubs为你的数据库的 KJp中国设计秀
String user="sa"; KJp中国设计秀
String password=""; KJp中国设计秀
Connection conn= DriverManager.getConnection(url,user,password);   KJp中国设计秀
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);   KJp中国设计秀
String sql="select * from test";   KJp中国设计秀
ResultSet rs=stmt.executeQuery(sql);   KJp中国设计秀
while(rs.next()) {%>   KJp中国设计秀
您的第一个字段内容为:<%=rs.getString(1)%>   KJp中国设计秀
您的第二个字段内容为:<%=rs.getString(2)%>   KJp中国设计秀
<%}%>   KJp中国设计秀
<%out.print("数据库操作成功,恭喜你");%>   KJp中国设计秀
<%rs.close();   KJp中国设计秀
stmt.close();   KJp中国设计秀
conn.close();   KJp中国设计秀
%>   KJp中国设计秀
</body>   KJp中国设计秀
</html> KJp中国设计秀
三、jsp连接DB2数据库 KJp中国设计秀
testdb2.jsp如下: KJp中国设计秀
<%@ page contentType="text/html;charset=gb2312"%>   KJp中国设计秀
<%@ page import="java.sql.*"%> KJp中国设计秀
<html>   KJp中国设计秀
<body>   KJp中国设计秀
<%Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();   KJp中国设计秀
String url="jdbc:db2://localhost:5000/sample"; KJp中国设计秀
//sample为你的数据库名 KJp中国设计秀
String user="admin"; KJp中国设计秀
String password=""; KJp中国设计秀
Connection conn= DriverManager.getConnection(url,user,password);   KJp中国设计秀
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);   KJp中国设计秀
String sql="select * from test";   KJp中国设计秀
ResultSet rs=stmt.executeQuery(sql);   KJp中国设计秀
while(rs.next()) {%>   KJp中国设计秀
您的第一个字段内容为:<%=rs.getString(1)%>   KJp中国设计秀
您的第二个字段内容为:<%=rs.getString(2)%>   KJp中国设计秀
<%}%>   KJp中国设计秀
<%out.print("数据库操作成功,恭喜你");%>   KJp中国设计秀
<%rs.close();   KJp中国设计秀
stmt.close();   KJp中国设计秀
conn.close();   KJp中国设计秀
%>   KJp中国设计秀
</body>   KJp中国设计秀
</html> KJp中国设计秀
四、jsp连接Informix数据库 KJp中国设计秀
testinformix.jsp如下: KJp中国设计秀
<%@ page contentType="text/html;charset=gb2312"%>   KJp中国设计秀
<%@ page import="java.sql.*"%> KJp中国设计秀
<html>   KJp中国设计秀
<body>   KJp中国设计秀
<%Class.forName("com.informix.jdbc.IfxDriver").newInstance();   KJp中国设计秀
String url =  KJp中国设计秀
"jdbc:informix-sqli://123.45.67.89:1533/testDB:INFORMIXSERVER=myserver; KJp中国设计秀
user=testuser;password=testpassword"; KJp中国设计秀
//testDB为你的数据库名 KJp中国设计秀
Connection conn= DriverManager.getConnection(url);   KJp中国设计秀
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);   KJp中国设计秀
String sql="select * from test";   KJp中国设计秀
ResultSet rs=stmt.executeQuery(sql);   KJp中国设计秀
while(rs.next()) {%>   KJp中国设计秀
您的第一个字段内容为:<%=rs.getString(1)%>   KJp中国设计秀
您的第二个字段内容为:<%=rs.getString(2)%>   KJp中国设计秀
<%}%>   KJp中国设计秀
<%out.print("数据库操作成功,恭喜你");%>   KJp中国设计秀
<%rs.close();   KJp中国设计秀
stmt.close();   KJp中国设计秀
conn.close();   KJp中国设计秀
%> KJp中国设计秀
</body> KJp中国设计秀
</html> KJp中国设计秀
五、jsp连接Sybase数据库 KJp中国设计秀
testMySQL.jsp如下: KJp中国设计秀
<%@ page contentType="text/html;charset=gb2312"%>   KJp中国设计秀
<%@ page import="java.sql.*"%> KJp中国设计秀
<html>   KJp中国设计秀
<body>   KJp中国设计秀
<%Class.forName("com.sybase.jdbc.SybDriver").newInstance();   KJp中国设计秀
String url =" jdbc:sybase:Tds:localhost:5007/tsdata"; KJp中国设计秀
//tsdata为你的数据库名 KJp中国设计秀
Properties sysProps = System.getProperties(); KJp中国设计秀
SysProps.put("user","userid"); KJp中国设计秀
SysProps.put("password","user_password"); KJp中国设计秀
Connection conn= DriverManager.getConnection(url, SysProps);   KJp中国设计秀
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);   KJp中国设计秀
String sql="select * from test";   KJp中国设计秀
ResultSet rs=stmt.executeQuery(sql);   KJp中国设计秀
while(rs.next()) {%>   KJp中国设计秀
您的第一个字段内容为:<%=rs.getString(1)%>   KJp中国设计秀
您的第二个字段内容为:<%=rs.getString(2)%>   KJp中国设计秀
<%}%>   KJp中国设计秀
<%out.print("数据库操作成功,恭喜你");%>   KJp中国设计秀
<%rs.close();   KJp中国设计秀
stmt.close();   KJp中国设计秀
conn.close();   KJp中国设计秀
%>   KJp中国设计秀
</body>   KJp中国设计秀
</html> KJp中国设计秀
六、jsp连接MySQL数据库 KJp中国设计秀
testmysql.jsp如下: KJp中国设计秀
<%@ page contentType="text/html;charset=gb2312"%>   KJp中国设计秀
<%@ page import="java.sql.*"%> KJp中国设计秀
<html>   KJp中国设计秀
<body>   KJp中国设计秀
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();   KJp中国设计秀
String url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1" KJp中国设计秀
//testDB为你的数据库名 KJp中国设计秀
Connection conn= DriverManager.getConnection(url);   KJp中国设计秀
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);   KJp中国设计秀
String sql="select * from test";   KJp中国设计秀
ResultSet rs=stmt.executeQuery(sql);   KJp中国设计秀
while(rs.next()) {%>   KJp中国设计秀
您的第一个字段内容为:<%=rs.getString(1)%>   KJp中国设计秀
您的第二个字段内容为:<%=rs.getString(2)%>   KJp中国设计秀
<%}%>   KJp中国设计秀
<%out.print("数据库操作成功,恭喜你");%>   KJp中国设计秀
<%rs.close();   KJp中国设计秀
stmt.close();   KJp中国设计秀
conn.close();   KJp中国设计秀
%>   KJp中国设计秀
</body>   KJp中国设计秀
</html> KJp中国设计秀
七、jsp连接PostgreSQL数据库 KJp中国设计秀
testmysql.jsp如下: KJp中国设计秀
<%@ page contentType="text/html;charset=gb2312"%>   KJp中国设计秀
<%@ page import="java.sql.*"%> KJp中国设计秀
<html>   KJp中国设计秀
<body>   KJp中国设计秀
<%Class.forName("org.postgresql.Driver").newInstance();   KJp中国设计秀
String url ="jdbc:postgresql://localhost/soft" KJp中国设计秀
//soft为你的数据库名 KJp中国设计秀
String user="myuser"; KJp中国设计秀
String password="mypassword"; KJp中国设计秀
Connection conn= DriverManager.getConnection(url,user,password);   KJp中国设计秀
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);   KJp中国设计秀
String sql="select * from test";   KJp中国设计秀
ResultSet rs=stmt.executeQuery(sql);   KJp中国设计秀
while(rs.next()) {%>   KJp中国设计秀
您的第一个字段内容为:<%=rs.getString(1)%>   KJp中国设计秀
您的第二个字段内容为:<%=rs.getString(2)%>   KJp中国设计秀
<%}%>   KJp中国设计秀
<%out.print("数据库操作成功,恭喜你");%>   KJp中国设计秀
<%rs.close();   KJp中国设计秀
stmt.close();   KJp中国设计秀
conn.close();   KJp中国设计秀
%>   KJp中国设计秀
</body>   KJp中国设计秀
</html> KJp中国设计秀
 KJp中国设计秀

本文引用地址:/bc/article_46766.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明