Jdbc faq (1)

Source: Internet
Author: User



What is JDBC? When will it be used?

The full name of JDBC is Java DataBase Connection, that is, Java DataBase Connection. We can use it to operate relational databases. The JDBC interface and related classes are in the java. SQL package and javax. SQL package. We can use it to connect to the database, execute SQL queries, store procedures, and process returned results.

The JDBC interface enables loose coupling between the Java program and the JDBC driver, making it easier to switch between different databases.

What are the different types of JDBC drivers?

There are four types of JDBC drivers. The Java program that interacts with the database is divided into two parts: one is the jdbc api and the other is the actual working driver.



A JDBC-ODBC Bridge plus ODBC Driver (type 1): it connects to the database using an ODBC Driver. You need to install ODBC to connect to the database. As a result, this method has been basically eliminated.

B Native API partly Java technology-enabled driver (Type 2): This driver adapts JDBC calls to the calls of the local database interface.

C Pure Java Driver for Database Middleware (Type 3): This Driver forwards JDBC calls to the Middleware server and connects to different databases. To use this type of driver, you need to deploy the middleware server. This method adds additional network calls, resulting in poor performance, so it is rarely used.

D Direct-to-Database Pure Java Driver (type 4): This Driver converts JDBC into the network protocol used by the Database. This solution is the simplest and suitable for connecting to the database through the network. However, to use this method, you need to select a specific Driver Based on Different databases. For example, OJDBC is the Oracle database driver developed by Oracle, and MySQL ctor/J is the MySQL database driver.

How does JDBC implement loose coupling between Java programs and JDBC drivers?

JDBC APIs use the reflection mechanism of Java to implement loose coupling between Java programs and JDBC drivers. If you look at a simple JDBC example, you will find that all operations are completed through the JDBC interface, and the driver will only appear when loaded through the Class. forName reflection mechanism.

I think this is one of the best practices of the reflection mechanism in the Java core library. It isolates applications and drivers, making database migration easier. Here we can see more examples of using JDBC.

What is a JDBC connection? How do I create a JDBC connection in Java?

A jdbc connection is a session established with the database server. You can think of it as a Socket connection to the database.

It is easy to create a JDBC connection. Only two steps are required:

A. Register and load the driver: When Class. forName () is used, the driver Class will be registered in DriverManager and loaded into memory.
B. Use DriverManager to obtain the connection object: Call DriverManager. getConnnection () and input the URL, user name, and password of the database connection.

{% Highlight java %}
Connection con = null;
Try {
// Load the Driver Class
Class. forName ("com. mysql. jdbc. Driver ");
// Create the connection now
Con = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/UserDB ",
"Pankaj ",
"Pankaj123 ");
} Catch (SQLException e ){
System. out. println ("Check database is UP and configs are correct ");
E. printStackTrace ();
} Catch (ClassNotFoundException e ){
System. out. println ("Please include JDBC MySQL jar in classpath ");
E. printStackTrace ();
}


What is JDBC DriverManager used?

JDBC DriverManager is a factory class through which we create database connections. When the JDBC Driver Class is loaded, it will register it to the DriverManager class. You can refer to the source code of the JDBC Driver Class for details.

Then we will pass the database configuration information to the DriverManager. getConnection () method. DriverManager will use the driver registered in it to obtain the database connection and return it to the called program.

In Java, how does one obtain information about the database server?

You can use DatabaseMetaData to obtain the server information. After the connection to the database is established, you can call the getMetaData () method to obtain the metadata of the database. There are many methods in DatabaseMetaData to obtain the database product name, version number, and configuration information.

DatabaseMetaData metaData = con.getMetaData();String dbProduct = metaData.getDatabaseProductName();


What is the JDBC Statement?

Statement is the interface used in JDBC to execute Database SQL query statements. Call the getStatement () method of the connection object to generate a Statement object. We can execute static SQL queries by calling its execute (), executeQuery (), and executeUpdate () methods.

Because SQL statements are passed in the program, if user input is not verified, it may cause SQL Injection problems. If you want to know
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.