Java and database connection method (i)

Source: Internet
Author: User
Tags getmessage connect odbc query readline table name
Data | database | Database Connection Activation Blog Since the recent comparison also did not write what original article, now while idle time to write several hair up. Some time ago in the study of Java and database connection, send a little experience up.

There are two main ways to connect a database with Java, one is to connect with the Jdbc-odbc bridge, the other is to connect with the corresponding driver provided by the manufacturer, first of all to talk about the first connection.

The JDBC-ODBC bridge is implemented using Jdbcodbc.class and a local library for accessing ODBC drivers. For Windows platforms, the local library is a dynamic Connection library DLL (JDBCODBC. DLL).

Because JDBC is designed to be very close to ODBC. Internally, the driver maps the JDBC method to an ODBC call, so that JDBC can interact with any available ODBC driver. The advantage of this bridge is that it gives JDBC the ability to access almost all databases at present. The prevailing mode is as shown in the figure:

Application---JDBC API---JDBC-ODBC---ODBC API---ODBC Layer---data source

The specific methods are:

First open the Control Panel management tool, open the data source (ODBC), and add the data source (i.e. the name of the database you are connecting to) to the User DSN, where the goodssupply database is assumed to be connected to SQL SERVER 2000. Name fill in the name of the database you want to connect to (goodssupply), and then gradually set, if you choose to use the Sql-server password authentication, you must enter the appropriate username and password to connect to the database. All the way to the next setup complete.

In Java to write programs to test, where my program is to allow users to enter any table name and the name of the column, the column of all data output. 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 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"); Using SQL-SERVER2000 Certification
DatabaseMetaData Dmd=con.getmetadata (); DMD for the corresponding condition 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 column name (end of NULL program):");
Cname=input.readline ();
if (Cname.equalsignorecase (""))
Break
command= "Select" +cname+ "from" +tablename;
Rs=sm.executequery (command); Execute Query
if (!rs.next ())
SYSTEM.OUT.PRINTLN ("Wrong table name or column name entered incorrectly");
else {
SYSTEM.OUT.PRINTLN ("Query result is:");
Todo
{
Result=rs.getstring (CName);
The database language is set to Chinese without converting the 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");
}
}
}




Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.