JDBC各个数据库 连接字符串
?
1.?MySQL(http://www.mysql.com)?mysql-connector-java-2.0.14-bin.jar?;
??Class.forName(?"org.gjt.mm.mysql.Driver"?); ?com.mysql.jdbc.Driver
??cn?=?DriverManager.getConnection(?"jdbc:mysql://MyDbComputerNameOrIP:3306/myDatabaseName",?sUsr,?sPwd?);?
jdbc:mysql://host:3306/db?useUnicode=true&characterEncoding=UTF-8;user=root;password=root?
2.?PostgreSQL(http://www.de.postgresql.org)?pgjdbc2.jar?;
??Class.forName(?"org.postgresql.Driver"?);?
??cn?=?DriverManager.getConnection(?"jdbc:postgresql://MyDbComputerNameOrIP/myDatabaseName",?sUsr,?sPwd?);?
jdbc:postgresql://127.0.0.1:5432/news?
3.?Oracle(http://www.oracle.com/ip/deploy/database/oracle9i/)?classes12.zip?;
??Class.forName(?"oracle.jdbc.driver.OracleDriver"?);?
??cn?=?DriverManager.getConnection(?"jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL",?sUsr,?sPwd?);?
4.?Sybase(http://jtds.sourceforge.net)?jconn2.jar?;
??Class.forName(?"com.sybase.jdbc2.jdbc.SybDriver"?);?
??cn?=?DriverManager.getConnection(?"jdbc:sybase:Tds:MyDbComputerNameOrIP:2638",?sUsr,?sPwd?);?
??//(Default-Username/Password:?"dba"/"sql")?
5.?Microsoft?SQLServer(http://jtds.sourceforge.net)?;
??Class.forName(?"net.sourceforge.jtds.jdbc.Driver"?);?
??cn?=?DriverManager.getConnection(?"jdbc:jtds:sqlserver://MyDbComputerNameOrIP:1433/master",?sUsr,?sPwd?);?
6.?Microsoft?SQLServer(http://www.microsoft.com)?;
??Class.forName(?"com.microsoft.jdbc.sqlserver.SQLServerDriver"?);?
??cn?=?DriverManager.getConnection(?"jdbc:microsoft:sqlserver://MyDbComputerNameOrIP:1433;databaseName=master",?sUsr,?sPwd?);?
7.?ODBC?
??Class.forName(?"sun.jdbc.odbc.JdbcOdbcDriver"?);?
??Connection?cn?=?DriverManager.getConnection(?"jdbc:odbc:"?+?sDsn,?sUsr,?sPwd?);?
8.DB2?Class.forName("com.ibm.db2.jdbc.net.DB2Driver");?
??String?url="jdbc:db2://192.9.200.108:6789/SAMPLE"?
??cn?=?DriverManager.getConnection(?url,?sUsr,?sPwd?);?
9.access由于access并不是作为一项服务运行,所以url的方法对他不适用。access可以通过odbc,也可以通过服务器映射路径的形式找到.mdb文件,参见http://rmijdbc.objectweb.org/Access/access.html
JDBC?API的使用方法
(1)登记并加载JDBC驱动程序;
两种方法:
Class.forName(String?drivername);
DriverManager.registerDriver(Driver?driver)
(2)建立与SQL数据库的连接;
DriverManager的getConnection()方法:
Connection?getConnection(String?url):url表示数据库地址字符串;
Connection?getConnection(String?url,String?user,String?pwd)
Connection?getConnection(String?url,Properties?info)
(3)传送一个SQL查询;
Connection的createStatement()方法:
Statement?createStatement();
Statement可以执行SQL语句,得到SQL查询结果。
(4)获得结果集。
Statement的执行SQL语句方法:
ResultSet?executeQuery(String?sql):执行select语句
int?executeUpdate(String?sql):执行更新语句,如insert,delete,update.
(5)检索查询结果。
ResultSet的方法:
boolean?next():没有行时返回false;
String?getString(String?columnName):返回列名对应的值。
?
?