13 Specifications of the Java EE (ii) JDBC and its use

Source: Internet
Author: User
Tags database issues odbc sql server driver what odbc java reference

I think we all know what ODBC is? ODBC (opendatabaseconnectivity. Open Database Interconnect (wosa,windowsopenservicesarchitecture) is an integral part of the database in Microsoft's Open Services architecture It establishes a set of specifications and provides a set of standard APIs (application programming interfaces) for access to the database.

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. It consists of a set of classes and interfaces written in the Java language. In fact, they're all the same. Provides a set of api! for connecting databases

Understanding JDBC

provides a unified way to access the database API, provide platform-independent access to the database, Transparent to proprietary database issues.

JDBC Driver

Let's take a look at a diagram where the Java reference program is connected to the database by driving it. Connect different databases to load different drivers!

For example, if we are connecting to a SQL Server database, we will load the SQL Server driver. First to download the corresponding driver package from the Internet Sqljdbc_4.0.2206.100_chs.tar assume that your database is the version number of SQL Server 2008, Then you can use this one (Sqljdbc4.jar) will be able to, of course, the assumption is that once, you can check I have not studied, but I want to say (you really persistent!) )。

Take a look at how to use Eclipse to connect to a SQL Server database! There is a picture of the truth.

First open eclipse, a random resume for a Java project. Then build a bag casually. The bottom of the package is creating a class!

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvemhhbmdob25namllmdmwmg==/font/5a6l5l2t/fontsize/400/fill/ I0jbqkfcma==/dissolve/70/gravity/center ">

Then right-click on the project name, select Build path and select Configure Build path ....

Then select the top libraries, click Add External JARs .... Then find the Sqljdbc4.jar file you downloaded from the Internet, be sure to download the correct file. Or you will be very remorseful!

So it is recommended that you download from here (Standard Sqljdbc4.jar).

After you have identified the Sqljdbc.jar file (be sure to look at the suffix), you are ready to click OK.

Then see, when writing code of the drive path is placed in the Java.sql.Driver file, then in order to prevent write wrong, can come here to paste!

And his real use of the driving class is placed in the COM.MICROSOFT.SQLSERVER.JDBC package, want to look directly into the inside, there are a lot of classes, what the role of the direct online check can be.

Then can be encoded, the first to introduce a few files before encoding, java.sql.Connection; Java.sql.DriverManager; Java.sql.PreparedStatement; Java.sql.ResultSet, you can use these classes directly in the program!

<span style= "FONT-SIZE:18PX;" >package com.tgb;import java.sql.connection;import Java.sql.drivermanager;import java.sql.PreparedStatement; Import Java.sql.resultset;public class Jdbcconn {public static void main (string[] args) throws Exception {//TODO Auto-gen Erated method Stubjdbcconn.test ();} public static void Test () throws exception{connection conn = null; PreparedStatement Ps=null; ResultSet rs= Null;try {//defines the path of the drive, which is in java.sql.Driver under services. String driverpath= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";//Load Driver Class.forName (DriverPath);// is to select the appropriate database driver from the current JVM's loader (possibly multiple) and get the connection Conn=drivermanager. getconnection ("jdbc:sqlserver://localhost:1433; Databasename=db_chargesystem "," sa "," 123456 "),//sql statement String sql =" SELECT * from T_userinfo ";//Send SQL statements to the connected database, The SQL statement has woken up in the PreparedStatement object to compile! PS = conn.preparestatement (SQL);//Returns the run result of the SQL statement. rs = Ps.executequery (); Work on the results, and now print them out and perform more complex operations in the real world! while (Rs.next ()) {System.out.println (rs.getstring (1));}} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();} finally{//Close all open connections, try {rs.close ();} catch (Exception E2) {//Todo:handle exceptione2.printstacktrace ();} Finally{try {ps.close ();} catch (Exception E3) {//Todo:handle exceptione3.printstacktrace ();} Finally{conn.close ();}}}} </span>

This is just a simple example. In the actual application we also have to do a good work on this method for further encapsulation. Then you can simplify the code very much.

The upper instance is a connection to the SQL Server database. The fact that other databases are connected is essentially the same. Although JDBC in the JAVA The language level is unified, but different databases There are still many differences.

To better achieve cross-database operations, Hibernate was born andhibernate is the JDBC of the re-encapsulation, the implementation of the database operation of a broader unified and better portability.

look forward to further study!

13 Specifications of the Java EE (ii) JDBC and its use

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.