There are two main ways to connect to the database with Java, one is to connect with the JDBC-ODBC bridge, the other is to use the corresponding driver provided by the relevant manufacturers
Program First, let's talk about the first connection.
The JDBC-ODBC bridge is implemented using jdbcodbc. class and a local library used to access the ODBC driver. For Windows, this local library is a dynamic Connection Library DLL (jdbcodbc. dll ).
JDBC is designed very close to ODBC. Internally, this driver maps the JDBC Method to the ODBC call, so that JDBC can interact with any available ODBC driver. The advantage of this bridge is that it enables JDBC to access almost all databases. Pass mode:
Application --- JDBC API---JDBC-ODBC---ODBC API---ODBC layer --- Data Source
The procedure is as follows:
First open the control panel management tool, open the data source (ODBC), add the data source (that is, the name of the database you want to connect to) in the user DSN ), it is assumed that the SQL Server 2000 goodssupply database is connected. Name enter the name of the database you want to connect to (goodssupply), and then gradually set, if you use the SQL-SERVER password authentication, you need to enter the corresponding user name and password to connect to the database. The next step is complete.
Write a program in Java for testing. Here, my program allows users to input any table name and column name, and output all the data in the column.Source codeAs follows:
1 Import java. Io. bufferedreader;
2 Import java. Io. inputstreamreader;
3 Import java. SQL. * ;
4
5 Public Class Odbcbridge {
6
7 Public Static Void Main (string [] ARGs) {
8 String URL = " JDBC: ODBC: goodssupply " ;
9 Statement Sm = Null ;
10 String command = Null ;
11 Resultset rs = Null ;
12 String tablename = Null ;
13 String cname = Null ;
14 String result = Null ;
15 Bufferedreader Input = New Bufferedreader ( New Inputstreamreader (system. In ));
16 Try {
17 Try {
18Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver");//Load driver
19} Catch (Classnotfoundexception E) {
20 System. Out . Println ( " Can not load JDBC-ODBC bridge driver " );
21 System. Err. Print ( " Classnotfoundexception: " );
22 System. Err. println (E. getmessage ());
23 }
24 Connection con = Drivermanager. getconnection (URL, " User " , " Password " ); // Use SQL-SERVER2000 Certification
25 Databasemetadata DMD = Con. getmetadata (); // The status of the connection is
26 System. Out . Println ( " Connected database: " + DMD. geturl ());
27 System. Out . Println ( " Driver: " + DMD. getdrivername ());
28 Sm = Con. createstatement ();
29 System. Out . Println ( " Input table name " );
30 Tablename = Input. Readline ();
31 While ( True ) {
32 System. Out . Println ( " Enter the column name (when it is null, the program ends ): " );
33 Cname = Input. Readline ();
34 If (Cname. equalsignorecase ( "" ))
35 Break ;
36 Command = " Select " + Cname + " From " + Tablename;
37 RS = Sm.exe cutequery (command ); // Execute Query
38 If ( ! Rs. Next ())
39 System. Out . Println ( " Incorrect table name or column name input " );
40 Else {
41 System. Out . Println ( " The query result is: " );
42 Do
43 {
44 Result = Rs. getstring (cname );
45 // Set the database language to Chinese without conversion Encoding
46 // Result = new string (result. getbytes ("ISO-8859-1"), "gb2312 ");
47 System. Out . Println (result );
48 } While (Rs. Next ());
49 }
50 }
51 } Catch (Sqlexception ex) {
52 System. Out . Println ( " Sqlexception: " );
53 While (EX ! = Null ) {
54System.Out. Println ("Message:"+Ex. getmessage ());
55Ex=Ex. getnextexception ();
56}
57 } Catch (Exception E) {
58System.Out. Println ("Ioexception");
59}
60 }
61 }
62
63