JAVA (2)--jdbc

Source: Internet
Author: User
Tags db2 informix sybase

When I first approached JDBC, I sometimes wondered why Java had to use JDBC instead of directly using ODBC, which was previously studied in VB, because ODBC was present before JDBC, so ODBC must be unable to complete certain operations, or it could not complete some operations very smoothly. So the advent of JDBC, then, in the use of Java, JDBC Cross ODBC What are the advantages?

So, let's briefly introduce ODBC:

The Open Database Interconnect (open DATABASECONNECTIVITY,ODBC) is an integral part of the database in the Microsoft Open Service Architecture (Wosa,windows Open Services Architecture), which sets up a set of specifications and provides a set of standard APIs (application programming interfaces) for access to the database. These APIs use SQL to complete most of their tasks. ODBC itself also provides support for the SQL language, allowing users to send SQL statements directly to ODBC. So far, Microsoft's ODBC is probably the most widely used API to access the relational database. It provides the ability to connect almost no matter what kind of platform, no matter what kind of database.

So why not just use ODBC directly from Java?

The answer is to be able to use ODBC from Java, but it is best to use the Jdbc-odbc Bridge with the help of JDBC. So, why do we need JDBC? To answer this question, there are several aspects:

1) ODBC is not intended to be used directly in Java. ODBC is a C language implementation of the API, from the Java program calls the local C program will bring a series of similar security, integrity, robustness of the shortcomings .

2) Secondly, it is not pleasant to implement ODBC translations from C-code ODBC to JAVAAPI in a completely accurate manner . For example, Java does not have pointers, and ODBC uses pointers in large numbers, including a very error-prone null pointer "void*". Therefore, it is natural for Java program apes to envision JDBC as an API that transforms ODBC into an object-oriented one.

3) ODBC is not easy to learn, it mixes simple features with complex features, and even has complex options for very easy queries. JDBC, on the other hand, retains the simplicity of simple things, but agrees with complex features .

4) JDBC This javaapi is necessary for pure Java scenarios. When using ODBC, people must install ODBC drives and drive managers on each client computer. Assuming that the JDBC drive is fully implemented in the Java language, the JDBC code can be downloaded and installed on its own initiative and secured , and this will adapt to whatever Java platform, from the network computer NC to the mainframe mainframe.

To summarize, Jdbcapi is the most straightforward Java interface that embodies the most basic abstraction of SQL. It is built on the basis of ODBC, so the program of familiarity with ODBC apes will find it easy to learn JDBC. JDBC maintains the basic design characteristics of ODBC. In fact, both of these interfaces are based on the X/OPENSQL call-level Interface (CLI). The biggest difference is that JDBC is based on Java's style and strengths, and reinforces Java's style and strengths.

So, to summarize the advantages of JDBC relative to ODBC, we can also summarize the characteristics of Java security, integrity, robustness, easy to learn, portability, which is also in the Java Summary (1) mentioned in some of the features, since JDBC has so many advantages, then we now introduce the system of JDBC .

JDBC (Java Data Base Connectivity,java database connection) is a Java API for running SQL statements that provides unified access to a variety of relational databases, consisting of a set of classes and interfaces written in the Java language. JDBC provides a benchmark to build more advanced tools and interfaces that enable database developers to write database applications.

how the JDBC API works


usage of JDBC:

1, load Drive.

Loading the driver requires only a very easy line of code. For example, if you want to use the Jdbc-odbc bridge driver, you can load it with the following code:

Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");

Your driver documentation will tell you which class name you should use. For example, suppose the class name is JDBC. DRIVERXYZ, you will load the driver with the code below:

Class.forName ("jdbc. Driverxyz ");

Once the Driver class is loaded, they can be used to establish a connection to the database.

2. Establish the connection

The second step is to establish a connection with the DBMS with the appropriate driver class. The following code is a general practice:

Connection con =drivermanager.getconnection (URL, "MyLogin", "MyPassword");

how JDBC establishes the connections for each database 1.oracle8/8i/9i database (thin mode)
   Class.forName ("Oracle.jdbc.driver.OracleDriver"). newinstance ();   String url= "Jdbc:oracle:thin: @localhost: 1521:ORCL";   ORCL the SID   String user= "test" for the database   ; String password= "Test";   Connection conn= drivermanager.getconnection (Url,user,password);


2.DB2 database
   Class.forName ("Com.ibm.db2.jdbc.app.DB2Driver"). newinstance ();   String url= "Jdbc:db2://localhost:5000/sample";   Sample for your database name   String user= "admin";   String password= "";   Connection conn= drivermanager.getconnection (Url,user,password);


3 ,Sql server7.0/2000 database
   Class.forName ("Com.microsoft.jdbc.sqlserver.SQLServerDriver"). newinstance ();   Stringurl= "Jdbc:microsoft:sqlserver://localhost:1433;databasename=mydb";   MyDB for database   String user= "sa";   String password= "";   Connection conn= drivermanager.getconnection (Url,user,password);


4.Sybase database
   Class.forName ("Com.sybase.jdbc.SybDriver"). newinstance ();   String url = "Jdbc:sybase:tds:localhost:5007/mydb";   MyDB for your database name   Properties sysprops = system.getproperties ();   Sysprops.put ("User", "userid");   Sysprops.put ("Password", "User_password");   Connection conn= drivermanager.getconnection (URL, sysprops);


5.Informix database
   Class.forName ("Com.informix.jdbc.IfxDriver"). newinstance ();   String url =   "jdbc:informix-sqli://123.45.67.89:1533/mydb:informixserver=myserver;   User=testuser;password=testpassword ";   MyDB is the database name   Connection conn= drivermanager.getconnection (URL);


6.MySQL database
   Class.forName ("Org.gjt.mm.mysql.Driver"). newinstance ();   String url= "Jdbc:mysql://localhost/mydb?user=soft&password=soft1234&useunicod   e=true& Characterencoding=8859_1 "   //mydb for database name   Connection conn= drivermanager.getconnection (URL);


7.PostgreSQL database
   Class.forName ("Org.postgresql.Driver"). newinstance ();   String url = "Jdbc:postgresql://localhost/mydb"   //mydb is the database name   String user= "MyUser";   String password= "MyPassword";   Connection conn= drivermanager.getconnection (Url,user,password);


To this, have a certain understanding of JDBC, want to know other Java EE specifications, please look forward to!


Related Article

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.