Method (Zhuanzhai) database connection with the JAVA

JAVA method of connecting to the database
                              

_2 with JAVA database connection There are two main ways, one is to use JDBC-ODBC bridge to connect the second is associated with the appropriate driver vendor to connect.

1, JDBC-ODBC bridge

JDBC-ODBC bridge JdbcOdbc.Class is achieved with a local library and for accessing the ODBC driver. For WINDOWS platform, the local database is a dynamic link library DLL (JDBCODBC.DLL).
Because ODBC and JDBC very close in design. Inside, the driver of the map to the JDBC ODBC call, so, you can interact with the JDBC and any available ODBC drivers. The advantage of this bridge is that it currently has the ability to make JDBC access to almost all databases. As shown in FIG passage way:

Application --- JDBC API --- JDBC-ODBC --- ODBC API --- ODBC data source layer ---.

JAVA implementation example: Assume the GoodsSupply connect SQL SERVER 2000 database.

URL = String "JDBC: ODBC: GoodsSupply";
the try {
the Class.forName ( "sun.jdbc.odbc.JdbcOdbcDriver"); // load drivers
} the catch (a ClassNotFoundException E) {
System.out.println ( "Can Not Load the Jdbc Bridge Driver -odbc ");
System.err.print (" a ClassNotFoundException: ");
System.err.println (e.getMessage ());
}
Connection the DriverManager.getConnection = CON (URL," the USER "," PASSWORD ") ; // authentication using SQL-SERVER2000
DatabaseMetaData dmd = con.getMetaData (); // DMD is a case where the respective connection
System.out.println ( "database connection:" + dmd.getURL ());
System.out.println ( "driver:" + dmd.getDriverName ());

Statement sm=con.createStatement();
String command="select "+cName+" from "+tableName;
ResultSet rs=sm.executeQuery(command); //执行查询

do
{
String Result = rs.getString (cName); // set Chinese language database, without transcoding
// Result = new new String (result.getBytes ( "the ISO-8859-1"), "GB2312");
the System. Out.println (Result);
} the while (rs.next ());

}
2. Use the vendor-supplied drivers

This implementation is the direct use vendor-supplied drivers database created with a dedicated network protocol through which you can directly JDBC API calls into direct network calls. This is called the general performance is better, but also practical in the easiest way. Because it step you need to install other libraries or middleware. Almost all database vendors provide this database for their database provides this JDBC driver, you can also obtain these drivers from third-party vendors.
From the web site http://industry.Java.sun.com/products/jdbc/drivers/ can see a list of all available drivers. The results are shown below: Application --- JDBC API --- --- driver data source.

You must first install the JDBC driver.

JAVA realize :( cases to SQL SERVER2000 example.)

the try
{
Class.forName ( "com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println ( "driver is loaded");
// SQL SERVER the landing approach must use SQL SERVER password authentication
Connection the DriverManager.getConnection = CON ( "JDBC: Microsoft: SQLServer: // SERVERNAME: 1433", "the USER", "PASSWORD");
con.setCatalog ( "GoodsSupply");
System.out.println ( "the OK, a successful connection to database ");
} the catch (Exception EX) {
ex.printStackTrace ();
}
the Statement con.createStatement SM = ();
String Command =" SELECT "cName + +" from "+ tableName;
the ResultSet sm.executeQuery RS = (Command) ;
do
{
Result = rs.getString (cName);
// Result = new new String (result.getBytes ( "ISO-8859-1"),"GB2312");
System.out.println(result);
}while(rs.next());
}

3. Summary of JAVA common database connection [ZZ from http://www.54bk.com/more.asp?name=syc&id=4184 ]

1. MySQL(http://www.mysql.com)mm.mysql-2.0.2-bin.jar
Class.forName( "org.gjt.mm.mysql.Driver" );
cn = DriverManager.getConnection( "jdbc:mysql://MyDbComputerNameOrIP:3306/myDatabaseName", sUsr, sPwd );

2. PostgreSQL(http://www.de.postgresql.org)pgjdbc2.jar
Class.forName( "org.postgresql.Driver" );
cn = DriverManager.getConnection( "jdbc:postgresql://MyDbComputerNameOrIP/myDatabaseName", sUsr, sPwd );

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

Excerpt:   http://blog.163.com/wangyongfei_2008@yeah/blog/static/172238329201151173525795/      

Reproduced in: https: //my.oschina.net/garyun/blog/602804

Guess you like

Origin blog.csdn.net/weixin_33860737/article/details/91774299