首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

java 联接各种数据库 总结

2012-10-31 
java 连接各种数据库 总结?Oracle8/8i/9i数据库(thin模式)Class.forName(oracle.jdbc.driver.OracleDrive

java 连接各种数据库 总结

?

Oracle8/8i/9i数据库(thin模式)

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID String user="test"; String password="test"; Connection conn= DriverManager.getConnection(url,user,password);

?

?

?

?

?

?

?

?

2、DB2数据库

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

?

?

?

?

?

?

?

?

3、SqlServer7.0/2000数据库

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb"; //mydb为数据库 String user="sa"; String password=""; Connection conn= DriverManager.getConnection(url,user,password);

?

驱动:

msbase.jar

mssqlserver.jar

msutil.jar

?

?

sqlserver 2005

?

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); String url="jdbc:sqlserver://localhost:1433;DatabaseName=mydb"; //mydb为数据库 String user="sa"; String password=""; Connection conn= DriverManager.getConnection(url,user,password);

?

驱动:sqljdbc.jar

?

?

?

?

?

?

?

?

4、Sybase数据库

Java连接Sybase数据库
5、Informix数据库

Class.forName("com.informix.jdbc.IfxDriver").newInstance(); String url = "jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver; user=testuser;password=testpassword"; //myDB为数据库名 Connection conn= DriverManager.getConnection(url);

?

?

?

?

?

?

?

?

6、MySQL数据库

驱动为mysql.jar
??String url ="jdbc:mysql://localhost/jpt?user=root&password=root&useUnicode=true&characterEncoding=8859_1" ;
???//myDB为数据库名 Connection conn= DriverManager.getConnection(url);
??Connection dbConn=null;
??try {
???Class.forName("com.mysql.jdbc.Driver").newInstance();
??} catch (ClassNotFoundException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??} catch (InstantiationException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??} catch (IllegalAccessException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
??try {
???dbConn = DriverManager.getConnection(url);
???
??? Statement stmt = null;???????? //数据库表达式
????????? ResultSet rs = null;???????????? //结果集
?????? stmt = dbConn.createStatement();
??????
????????? //4. 执行SQL
???????? // rs = stmt.executeQuery("SELECT 序号 AS ID , 车号 AS CH,日期 AS RQ, 时间 AS SJ,毛重 AS MZ,皮重 AS PZ,净重 JZ, 发货 AS FZ,收货 AS DZ, 货名 AS RLCP, 扣杂 AS KGL ,台号 AS HQH FROM SAVER? where 序号>'300026876' ");
?????? rs = stmt.executeQuery("SELECT * FROM zz_userlist ");
?????? SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
????????? //5. 现实结果集里面的数据
?????? java.sql.ResultSetMetaData md = rs.getMetaData();
????????? while(rs.next()) {
??????????? System.out.print("\n");
??????????? for(int i=1; i<= md.getColumnCount(); i++) {
????????????? System.out.print(rs.getString(i) + " | ");
??????????? }
????????? }
????????? rs.close();
????
??} catch (SQLException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
??System.out.println("Connection Successful!");
???

?

?

?

?

?

7、PostgreSQL数据库

Class.forName("org.postgresql.Driver").newInstance(); String url ="jdbc:postgresql://localhost/myDB" //myDB为数据库名 String user="myuser"; String password="mypassword"; Connection conn= DriverManager.getConnection(url,user,password);

?

?

?

?

?

?

?

?

Thirdly, you can use 'Class.forName("com.hxtt.sql.paradox.ParadoxDriver").newInstance();' or Class.forName("com.hxtt.sql.paradox.ParadoxDriver");' to load this driver.

Fourth, if you have used other JDBC driver, you only need to know the correct URL format for DriverManager.getConnection(url,"",""); You can find the Paradox URL format below. If you were a Java novice, please read also other Java examples code in Paradox_JDBC30demo.zip, Paradox_JDBC20demo.zip or Paradox_JDBC12demo.zip.
Paradox URL format:

??????? Embedded:

?

?

?

?

?

?

??????????????? jdbc:paradox:[//]/[DatabasePath][?prop1=value1[;prop2=value2]] (You can omit that "//" characters sometimes)

?

?

?

?

?

?

??????????????????????? For example:

?

?

?

?

?

?

??????????????????????????????? "jdbc:paradox:/."

?

?

?

?

?

?

??????????????????????????????? "jdbc:paradox:/c:/data" for Windows driver

?

?

?

?

?

?

??????????????????????????????? "jdbc:paradox:///c:/data" for Windows driver

?

?

?

?

?

?

??????????????????????????????? "jdbc:paradox:////usr/data" for unix or linux

?

?

?

?

?

?

??????????????????????????????? "jdbc:paradox://///192.168.10.2/sharedir" for UNC path

?

?

?

?

?

?

????????????? ??????????????????"jdbc:paradox:/./data"

?

?

?

?

?

?

??????? Remote Access (client/server mode):

?

?

?

?

?

?

??????????????? jdbc:paradox://host:port/[DatabasePath]

?

?

?

?

?

?

??????????????????????? For example: "jdbc:paradox://domain.com:3099/c:/data" if one ParadoxServer is run on the 3099 port of domain.com

?

?

?

?

?

?

??????? Compressed Database:(.ZIP, .JAR, .GZ, .TAR, .BZ2, .TGZ, .TAR.GZ, .TAR.BZ2)

?

?

?

?

?

?

????????????????jdbc url format is the same as embedded url and remote url.

?

?

?

?

?

?

??????????????????????? For example:

?

?

?

?

?

?

??????????????????????????????? "jdbc:paradox:/c:/test/testparadox.zip

?

?

?

?

?

?

??????? Memory-only Database:

?

?

?

?

?

?

??????????????? jdbc:paradox:/_memory_/

?

?

?

?

?

?

??????? URL Database:(http protocol, https protocol, ftp protocol)

?

?

?

?

?

?

??????????????? jdbc:paradox:http://httpURL

?

?

?

?

?

?

??????????????? jdbc:paradox:https://httpsURL

?

?

?

?

?

?

??????????????? jdbc:paradox:ftp://ftpURL

?

?

?

?

?

?

??????????????????????? For example:

?

?

?

?

?

?

??????????????????????????????? "jdbc:paradox:http://www.hxtt.com/test" ////Note: FTP site's user/password should be set in ftpURL, and cannot be set in JDBC connection property because user/password JDBC connection property belongs to server/client connection.

?

?

?

?

?

?

??????? SAMBA Database:(smb protocol)

?

?

?

?

?

?

??????????????? jdbc:paradox:smb://[[[domain;]username[:password]@]server[:port]/[[share/[dir/]file]]][?[param=value]]

?

?

?

?

?

?

???????? ???????????????For example:

?

?

?

?

?

?

??????????????????????????????? "jdbc:paradox:smb://test1:123@100.100.13.94/paradoxfiles".zone" ////Note: SAMBA user/password should be set in SMB URL, and cannot be set in JDBC connection property because user/password JDBC connection property belongs to server/client connection.

?

?

?

?

?

?

??????? Free JDBC url:(Warning: only use it for special project)

?

?

?

?

?

?

??????????????? jdbc:paradox:/" or "jdbc:paradox:///". Then you can use some full UNC path names in SQL to visit anywhere where your Java VM has right to access.

?

?

?

?

?

?

??????????????????????? For instance:

?

?

?

?

?

?

??????????????????????????????? select * from \\amd2500\e$\paradoxfiles\test;

?

?

?

?

?

?

??????????????????????????????? elect * from "\\amd2500\d$\paradoxiles".test;

?

?

?

?

?

?

??????????????????????????????? select * from ".".test;

?

?

?

?

?

?

?

?

?

?

?

?

?

???????? HXTT Paradox supports seamlessly data mining on memory-only table, physical table, url table, compressed table, SAMBA table in a sql. More details

?

?

?

?

?

?

???????? is in Advanced Programming chapter.

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

2:远程访问数据库

?

CLASSPATH=%CLASSPATH%; E:\RmiJdbc\dist\lib\RmiJdbc.jar;

注意:E:\RmiJdbc\dist\lib\RmiJdbc.jar为Rmijdbc.jar放置的目录

java org.objectweb.rmijdbc.RJJdbcServer

?

我写的一个BAT文件后台运行服务

java连接access服务.bat

?

?

?

?

?

?

?1:访问本发数据库

热点排行