« ASP过滤SQL非法字符并格式化html代码(不错的功能函数集)« »java_jsp连接各种数据库方法的简单示例 »
java-jsp连接数据库大全(个人收集)
现在有好多初学jsp的网友经常会问数据库怎么连接啊,怎么老出错啊?所以我集中的在这写篇文章供大家参考,其实这种把数据库逻辑全部放在jsp里未必是好的做法,但是有利于初学者学习,所以我就这样做了,当大家学到一定程度的时候,可以考虑用MVC的模式开发。
       最主要的是驱动及URL不同.下面不列出各数据库的驱动及URL
<%@ page contentType="text/html;charset=gb2312"%> 
<%@ page import="java.sql.*"%> 
一、         jsp连接Oracle8/8i/9i数据库(用thin模式) 
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);
 二、jsp连接Sql Server7.0/2000数据库 
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
url="jdbc:microsoft: sqlserver://localhost:1433;DatabaseName=pubs"; 
三、jsp连接DB2数据库 
Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance(); 
String url="jdbc: db2://localhost:5000/sample"; 
四、jsp连接Informix数据库 
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); 
五、jsp连接Sybase数据库 
Class.forName("com.sybase.jdbc.SybDriver").newInstance(); 
String url =" jdbc:sybase:Tds:localhost:5007/tsdata"; //tsdata
为你的数据库名 
Properties sysProps = System.getProperties(); 
SysProps.put("user","userid"); 
SysProps.put("password","user_password"); 
Connection conn= DriverManager.getConnection(url, SysProps);

六、jsp连接MySQL数据库 
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); 
String url ="jdbc: mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1" //testDB
为你的数据库名
 
Connection conn= DriverManager.getConnection(url);  


七、jsp连接PostgreSQL数据库 
Class.forName("org.postgresql.Driver").newInstance(); 
String url ="jdbc: postgresql://localhost/soft" 


: jsp连接access数据库不需要装jdbc

<< String strDirPath=application.getRealPath(request.getRequestURI());
strDirPath=strDirPath.substring(0,strDirPath.lastIndexOf('\\'))+"\\"; >>
<<String spath="book\\data\\#data.mdb";   //注意由网站根到文件的路径
String dbpath = application.getRealPath(spath); //转化成物理路径>>
String strurl="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\books.mdb; USERID=?;PWD=?";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection(strurl);
Statement stmt=conn.createStatement();


 
本文原创,转载请注明来自(巴士飞扬,www.busfly.cn)

 


Tags: JAVA   |

原创文章如转载,请注明:转载自:巴士飞扬-技术BLOG : http://www.busfly.net/

本文链接地址:http://www.busfly.net/post/36.html

如果你喜欢本文,请顶一下,支持我,你的支持是我继续发好文章的最大动力。谢谢。
好东西需要分享,快把本文发给你的朋友吧~!~

     
相关文章:
JAVA如何定义使用二维数组  (2007-8-7 14:56:41)




◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
网站分类
分类最近文章
最近发表
最新评论及回复
最近留言
热文排行
随机推荐文章
Powered By Z-Blog   STYLE by busfly . FatMouse
Copyright © 2007 巴士飞扬技术博客. . 沪ICP备07027972号. 会员群1(J2EE为主):3769186.