After the blog is activated, since there is no original article, I will write a few issues when I am free. Summary Time is studying the connection between Java and the database, issued a little experience.
There are two main ways to connect the database with the Java, one is to connect with the JDBC-ODBC bridge, and the other is to connect with the corresponding drivers provided by the relevant vendors, first talk about the first connection.
The JDBC-ODBC bridge is implemented with JDBCODBC.CLASS and a local library for accessing the ODBC driver. For Windows platform, the local library is a dynamic connection library DLL (JDBCODBC.DLL).
Since JDBC is close to ODBC in design. Internally, this driver maps the JDBC's method to ODBC calls, so that JDBC can interact with any ODBC drivers for any available ODBC. The advantage of this bridge is that it makes JDBC currently have the ability to access almost all databases. The pass mode is shown in the figure:
Application - JDBC API --- JDBC-ODBC --- ODBC API --- ODBC Layer --- Data Source
The specific method of operation is:
First open the management tool of the control panel, open the data source (ODBC), add a data source in the user DSN (ie, the name of the database you want to connect), where you assume the goodssupply database for SQL Server 2000. The name fills in the name of the database you want to connect, then step-by-step, if you use SQL-Server password authentication, you will enter the corresponding username and password to connect to the database. All the next step setting is completed.
Testing programs in Java, here my program is to allow users to enter any table name and with column name, and output all data of the column. The source code is as follows:
Import java.io.bufferedreader; import java.io.inputstreamReader; import java.sql. *;
Public class odbcbridge {
public static void main (String [] args) {String url = "jdbc: odbc: GoodsSupply"; Statement sm = null; String command = null; String tableName = null;; String cName = null; ResultSet rs = null String result = null; BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); try {try {Class.forName ( "sun.jdbc.odbc.JdbcOdbcDriver"); // load drivers} catch (ClassNotFoundException e) {System.out .println ("Can not load jdbc-odbc bridge driver"); System.err.Print ("ClassNotFoundExce:"); System.err.Println (E.GetMessage ());} Connection Con = DriverManager.getConnection (URL, "User", "password"); // uses SQL-Server2000 authentication DatabaseMetadata DMD = con?getMetadata (); // DMD is the corresponding case of the connection System.out.Println ("Connected Database:" DMD.GetURL ))); System.out.println ("driver:" DMD.GetdriverName ()); SM = con.createstatement (); system.out.println ("Input Table Name); Tablename = INPUT.READLINE () WHILE (TRUE) {system.out.println ("Enter the column name (end for an empty program):"); cname = INPUT.READLINE (); if (cname.equalsignorecase (")) BR Eak; command = "select" CNAME "from" Tablename; rs = sm.executeQuery (Command); // Execute Query if (! rs.next ()) System.out.println ("Table name or column name Incorrectly "); else {system.out.println (" Query results are: "); do {result = rs.getstring (cname); // Database language set to Chinese, no conversion encoding // result = new string Result.getbytes ("ISO-8859-1"), "GB2312"); System.out.Println (Result);}}}} catch (sqlexception ex) {system.out. Println ("Sqlexception:"); while (ex! = null) {system.out.println ("