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

Source: Internet
Author: User

I think we all know what ODBC is? ODBC (opendatabaseconnectivity, open Database interconnect) is an integral part of the database in the Microsoft Open Service Architecture (wosa,windowsopenservicesarchitecture), which sets up a set of specifications and provides a set of standard APIs (application programming interfaces) for database access.

JDBC (Java Data Base Connectivity,java database connection) is a Java API for executing 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. 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's APIs, provides platform-independent database access, 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, then we need to load the SQL Server driver, first to download the appropriate driver package from the Internet Sqljdbc_4.0.2206.100_chs.tar if your database is a later version of SQL Server 2008, Then you can use this one (Sqljdbc4.jar) on it, of course, if it is the previous, you can check I have not studied, but I want to say (you really persistent!). )。

Take a look at using Eclipse to connect to SQL Server database usage! There is a picture of the truth!

First open eclipse, a random resume a Java project, and then randomly build a package, the bottom of the package is to create a class!

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 you downloaded from the Internet good Sqljdbc4.jar file, be sure to download the correct file, otherwise you will be very regret! So it is recommended that you download from here (Standard Sqljdbc4.jar)!

When you are sure that you have Sqljdbc.jar files (be sure to look at the suffix name), you can click OK.

Then, when writing code, the driver's path is placed in the Java.sql.Driver file, then in order to prevent errors, you can paste it here! And his real use of the driving class is placed in the COM.MICROSOFT.SQLSERVER.JDBC bag, want to look directly into the inside, there are a lot of, what the role of direct to the 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 execution 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 have to do a good work on this method for further encapsulation! Then you can simplify the code a lot!

The upper instance is a connection to the SQL Server database, but the connection to the other database is essentially the same as this one. despite 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.