选择显示字体大小

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=&quot;text/html;charset=gb2312&quot;%>
<%@ page import=&quot;java.sql.*&quot;%>
<html>
<body>
<%class.forname(&quot;oracle.jdbc.driver.oracledriver&quot;).newinstance();
string url=&quot;jdbc:oracle:thin:@localhost:1521:orcl&quot;;
//orcl为你的数据库的sid
string user=&quot;scott&quot;;
string password=&quot;tiger&quot;;
connection conn= drivermanager.getconnection(url,user,password);
statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
string sql=&quot;select * from test&quot;;
resultset rs=stmt.executequery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getstring(1)%>
您的第二个字段内容为:<%=rs.getstring(2)%>
<%}%>
<%out.print(&quot;数据库操作成功,恭喜你&quot;);%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
二、jsp连接sql server7.0/2000数据库
testsqlserver.jsp如下:
<%@ page contenttype=&quot;text/html;charset=gb2312&quot;%>
<%@ page import=&quot;java.sql.*&quot;%>
<html>
<body>
<%class.forname(&quot;com.microsoft.jdbc.sqlserver.sqlserverdriver&quot;).newinstance();
string url=&quot;jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs&quot;;
//pubs为你的数据库
string user=&quot;sa&quot;;
string password=&quot;&quot;;
connection conn= drivermanager.getconnection(url,user,password);
statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
string sql=&quot;select * from test&quot;;
resultset rs=stmt.executequery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getstring(1)%>
您的第二个字段内容为:<%=rs.getstring(2)%>
<%}%>
<%out.print(&quot;数据库操作成功,恭喜你&quot;);%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
三、jsp连接db2数据库
testdb2.jsp如下:
<%@ page contenttype=&quot;text/html;charset=gb2312&quot;%>
<%@ page import=&quot;java.sql.*&quot;%>
<html>
<body>
<%class.forname(&quot;com.ibm.db2.jdbc.app.db2driver &quot;).newinstance();
string url=&quot;jdbc:db2://localhost:5000/sample&quot;;
//sample为你的数据库
string user=&quot;admin&quot;;
string password=&quot;&quot;;
connection conn= drivermanager.getconnection(url,user,password);
statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
string sql=&quot;select * from test&quot;;
resultset rs=stmt.executequery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getstring(1)%>
您的第二个字段内容为:<%=rs.getstring(2)%>
<%}%>
<%out.print(&quot;数据库操作成功,恭喜你&quot;);%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
四、jsp连接informix数据库
testinformix.jsp如下:
<%@ page contenttype=&quot;text/html;charset=gb2312&quot;%>
<%@ page import=&quot;java.sql.*&quot;%>
<html>
<body>
<%class.forname(&quot;com.informix.jdbc.ifxdriver&quot;).newinstance();
string url =
&quot;jdbc:informix-sqli://123.45.67.89:1533/testdb:informixserver=myserver;
user=testuser;password=testpassword&quot;;
//testdb为你的数据库
connection conn= drivermanager.getconnection(url);
statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
string sql=&quot;select * from test&quot;;
resultset rs=stmt.executequery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getstring(1)%>
您的第二个字段内容为:<%=rs.getstring(2)%>
<%}%>
<%out.print(&quot;数据库操作成功,恭喜你&quot;);%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
五、jsp连接sybase数据库
testmysql.jsp如下:
<%@ page contenttype=&quot;text/html;charset=gb2312&quot;%>
<%@ page import=&quot;java.sql.*&quot;%>
<html>
<body>
<%class.forname(&quot;com.sybase.jdbc.sybdriver&quot;).newinstance();
string url =&quot; jdbc:sybase:tds:localhost:5007/tsdata&quot;;
//tsdata为你的数据库
properties sysprops = system.getproperties();
sysprops.put(&quot;user&quot;,&quot;userid&quot;);
sysprops.put(&quot;password&quot;,&quot;user_password&quot;);
connection conn= drivermanager.getconnection(url, sysprops);
statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
string sql=&quot;select * from test&quot;;
resultset rs=stmt.executequery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getstring(1)%>
您的第二个字段内容为:<%=rs.getstring(2)%>
<%}%>
<%out.print(&quot;数据库操作成功,恭喜你&quot;);%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
六、jsp连接mysql数据库
testmysql.jsp如下:
<%@ page contenttype=&quot;text/html;charset=gb2312&quot;%>
<%@ page import=&quot;java.sql.*&quot;%>
<html>
<body>
<%class.forname(&quot;org.gjt.mm.mysql.driver&quot;).newinstance();
string url =&quot;jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useunicode=true&characterencoding=8859_1&quot;
//testdb为你的数据库
connection conn= drivermanager.getconnection(url);
statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
string sql=&quot;select * from test&quot;;
resultset rs=stmt.executequery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getstring(1)%>
您的第二个字段内容为:<%=rs.getstring(2)%>
<%}%>
<%out.print(&quot;数据库操作成功,恭喜你&quot;);%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
七、jsp连接postgresql数据库
testmysql.jsp如下:
<%@ page contenttype=&quot;text/html;charset=gb2312&quot;%>
<%@ page import=&quot;java.sql.*&quot;%>
<html>
<body>
<%class.forname(&quot;org.postgresql.driver&quot;).newinstance();
string url =&quot;jdbc:postgresql://localhost/soft&quot;
//soft为你的数据库
string user=&quot;myuser&quot;;
string password=&quot;mypassword&quot;;
connection conn= drivermanager.getconnection(url,user,password);
statement stmt=conn.createstatement(resultset.type_scroll_sensitive,resultset.concur_updatable);
string sql=&quot;select * from test&quot;;
resultset rs=stmt.executequery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getstring(1)%>
您的第二个字段内容为:<%=rs.getstring(2)%>
<%}%>
<%out.print(&quot;数据库操作成功,恭喜你&quot;);%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>

  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons