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. The traffic mode is shown in the following figure:
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
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service