After activating a blog, I haven't written any original articles recently. Now I can write a few articles at leisure. Some time ago, I was studying the connection between Java and databases, and I had some experience.
There are two main ways to connect the database with Java, one is to connect with the JDBC-ODBC bridge, the second is to connect with the corresponding driver provided by the relevant manufacturers, first 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. 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;
Resultset rs = NULL;
String tablename = NULL;
String cname = NULL;
String result = NULL;
Bufferedreader input = new bufferedreader (New inputstreamreader (system. In ));
Try {
Try {
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver"); // load the driver
} Catch (classnotfoundexception e ){
System. Out. println ("can not load JDBC-ODBC bridge driver ");
System. Err. Print ("classnotfoundexception :");
System. Err. println (E. getmessage ());
}
Connection con = drivermanager. getconnection (URL, "user", "password"); // use SQL-SERVER2000 Authentication
Databasemetadata DMD = con. getmetadata (); // the result of the connection is
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 ("Name of the input column (when it is null, the program ends ):");
Cname = input. Readline ();
If (cname. canonical signorecase (""))
Break;
Command = "select" + cname + "from" + tablename;
Rsw.sm.exe cutequery (command); // execute the query
If (! Rs. Next ())
System. Out. println ("Incorrect table name or column name input ");
Else {
System. Out. println ("query result :");
Do
{
Result = Rs. getstring (cname );
// Set the database language to Chinese without conversion Encoding
// Result = new string (result. getbytes ("ISO-8859-1"), "gb2312 ");
System. Out. println (result );
} While (Rs. Next ());
}
}
} Catch (sqlexception ex ){
System. Out. println ("sqlexception :");
While (ex! = NULL ){
System. Out. println ("message:" + ex. getmessage ());
Ex = ex. getnextexception ();
}
} Catch (exception e ){
System. Out. println ("ioexception ");
}
}
}