熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java高級技術 >> 正文

java 連接各類數據庫

2022-06-13   來源: Java高級技術 

  Oracle/i/i數據庫(thin模式)
ClassforName(oraclejdbcdriverOracleDriver)newInstance();
String url=jdbc:oracle:thin:@localhost::orcl;
//orcl為數據庫的SID
String user=test;
String password=test;
Connection conn= DriverManagergetConnection(urluserpassword);
DB數據庫
ClassforName(comibmdbjdbcappDBDriver )newInstance();
String url=jdbc:db://localhost:/sample;
//sample為你的數據庫名
String user=admin;
String password=;
Connection conn= DriverManagergetConnection(urluserpassword);
Sql Server/數據庫
ClassforName(commicrosoftjdbcsqlserverSQLServerDriver)newInstance();
String url=jdbc:microsoft:sqlserver://localhost:;DatabaseName=mydb;
//mydb為數據庫
String user=sa;
String password=;
Connection conn= DriverManagergetConnection(urluserpassword);
Sybase數據庫
ClassforName(comsybasejdbcSybDriver)newInstance();
String url = jdbc:sybase:Tds:localhost:/myDB;
//myDB為你的數據庫名
Properties sysProps = SystemgetProperties();
SysPropsput(useruserid);
SysPropsput(passworduser_password);
Connection conn= DriverManagergetConnection(url SysProps);
Informix數據庫
ClassforName(rmixjdbcIfxDriver)newInstance();
String url =
jdbc:informixsqli://:/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword;
//myDB為數據庫名
Connection conn= DriverManagergetConnection(url);
MySQL數據庫
ClassforName(orggjtmmmysqlDriver)newInstance();
String url =jdbc:mysql://localhost/myDB?user=soft&password=soft&useUnicode=true&characterEncoding=_
//myDB為數據庫名
Connection conn= DriverManagergetConnection(url);
PostgreSQL數據庫
ClassforName(orgpostgresqlDriver)newInstance();
String url =jdbc:postgresql://localhost/myDB
//myDB為數據庫名
String user=myuser;
String password=mypassword;
Connection conn= DriverManagergetConnection(urluserpassword);

  JDBCODBC橋
ClassforName(sunjdbcodbcJdbcOdbcDriver);
Connection con=DriverManagergetConnection(jdbc:odbc:jsp);
jsp為建立的odbc數據源名事先要先將SQL server的表設置為數據源管理工具數據源odbc裡用系統DNS添加

  Oracle/i/i數據庫(thin模式)
//import javasql*;
ClassforName(oraclejdbcdriverOracleDriver)newInstance();
String url=jdbc:oracle:thin:@localhost::orcl; //orcl為數據庫的SID
String user=test;
String password=test;
Connection conn= DriverManagergetConnection(urluserpassword);
Statement stmtNew=conncreateStatement();
DB數據庫
//import javasql*;
ClassforName(comibmdbjdbcappDBDriver )newInstance();
String url=jdbc:db://localhost:/sample; //sample為你的數據庫名
String user=admin;
String password=;
Connection conn= DriverManagergetConnection(urluserpassword);
Statement stmtNew=conncreateStatement();
Sql Server/數據庫
//import javasql*;
ClassforName(commicrosoftsqlserverjdbcSQLServerDriver)newInstance();
//String url=jdbc:microsoft:sqlserver://localhost:;DatabaseName=db; //
String url=jdbc:sqlserver://localhost:;DatabaseName=db; //
//db為數據庫名
String user=sa;
String password=;
Connection conn= DriverManagergetConnection(urluserpassword);
Statement stmtNew=conncreateStatement();
Sybase數據庫
//import javasql*;
ClassforName(comsybasejdbcSybDriver)newInstance();
String url = jdbc:sybase:Tds:localhost:/myDB;//myDB為你的數據庫名
Properties sysProps = SystemgetProperties();
SysPropsput(useruserid);
SysPropsput(passworduser_password);
Connection conn= DriverManagergetConnection(url SysProps);
Statement stmtNew=conncreateStatement();
Informix數據庫
//import javasql*;
ClassforName(rmixjdbcIfxDriver)newInstance();
String url = jdbc:informixsqli://:/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword; //myDB為數據庫名
Connection conn= DriverManagergetConnection(url);
Statement stmtNew=conncreateStatement();
MySQL數據庫
//import javasql*;
//ClassforName(orggjtmmmysqlDriver)newInstance();
ClassforName(commysqljdbcDriver);
//String url =jdbc:mysql://localhost/myDB?user=soft&password=soft&useUnicode=true&characterEncoding=_;
String url =jdbc:mysql://localhost:/myDB;
//myDB為數據庫名
Connection conn= DriverManagergetConnection(urlrootroot);
Statement stmtNew=conncreateStatement();
PostgreSQL數據庫
//import javasql*;
ClassforName(orgpostgresqlDriver)newInstance();
String url =jdbc:postgresql://localhost/myDB //myDB為數據庫名
String user=myuser;
String password=mypassword;
Connection conn= DriverManagergetConnection(urluserpassword);
Statement stmtNew=conncreateStatement();
access數據庫直連用ODBC的
//import javasql*;
ClassforName(sunjdbcodbcJdbcOdbcDriver) ;
String url=jdbc:odbc:Driver={MicroSoft Access Driver (*mdb)};DBQ=+applicationgetRealPath(/Data/ReportDemomdb);
Connection conn = DriverManagergetConnection(urlsa);
Statement stmtNew=conncreateStatement();
程序計時
long time=SystemcurrentTimeMillis();
long time=SystemcurrentTimeMillis();
long interval=timetime;
延時
try {
Threadsleep(IntegerParse(%%));
} catch(InterruptedException e) {
eprintStackTrace();
}
連接Excel文件
//import javasql*;
ClassforName(sunjdbcodbcJdbcOdbcDriver);
String url = jdbc:odbc:driver={Microsoft Excel Driver (*xls)};DBQ=D:\\myDBxls; // 不設置數據源
String user=myuser;
String password=mypassword;
Connection conn= DriverManagergetConnection(urluserpassword);
Statement stmtNew=conncreateStatement();


From:http://tw.wingwit.com/Article/program/Java/gj/201311/27386.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.