JAVA EE Learning Note [V3 JDBC Connection Database]

Source: Internet
Author: User
Tags odbc mysql version

Going on

In the previous two sections the power a dangerous the servlet and the JSP wave. However, these things from our actual development still have a long distance (at least the job can not be completed =-=), so want to go further close to the actual development, it must have a certain degree of knowledge of the database to grasp and understand (here for a moment, the database is a branch of discipline, minutes to make big), I am temporarily learning and understanding the technology of Java access to the database, JDBC.

Introduction to JDBC

background data for commercial applications is generally stored in the database, and it is clear that the database can be accessed through Java code.

In the Java technology family, the technology that accesses the database is called JDBC (Java datebase Connectivity), which provides a series of APIs that allow code written in the Java language to connect to the database, add, delete, modify, and query the data of the database.

JDBC Programming Interface

JDBC provides two programming interfaces: the JDBC API for common programmers for database connection operations, and the JDBC Driver Interface for JDBC driver development.

The JDBC API only cares about Java's abstract interface for invoking SQL, regardless of which method is used, and the JDBC driver completes the database call.

As a result, our ordinary programmers can manipulate different databases simply by composing a program in accordance with the JDBC API and then building different JDBC drivers.

For different types of databases, the concept of "driver" is provided in the JDBC mechanism. The program only needs to use the driver provided by the database vendor to connect to the database and manipulate it.

JDBC Drive Program

There are two main types of JDBC drivers:

JDBC Driver: The JDBC driver that most database systems implement, which JSP can use to access the database directly.

ODBC Bridge: Some database systems do not provide JDBC drivers directly, but they often provide ODBC drivers, and ODBC is a Windows-system Open Database Interconnect interface designed and developed by Microsoft Corporation (open databases Co nnectivity). JSPs can be accessed through Sun's Jdbc-odbc bridge.

JDBC API

The JDBC API is called by the user to implement basic database operations.

These include four classes: Java.sql.Connection: Responsible for connecting to the database

Java.sql.Statement: Responsible for executing database SQL statements

Java.sql.ResultSet: Responsible for storing query results

Java.sql.DriverManager: Responsible for loading drivers

The general process of using JDBC is to develop the following

After understanding JDBC We continue to follow the Convention (MyEclipse CI4 for example):

Here we use the IDE's own Derby database =-=

After you create the Web Service project, right-click the project to open the properties (you have to add the Derby.jar manually.)

Here I put my hands on the cheap, and remember to add Derbyclient.jar to. classpath

Then edit the properties of the Derby database

After the code is done (put in the JSP):

<%@ page language= "Java"Import= "java.util.*,java.sql.*" pageencoding= "Iso-8859-1"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >This is my JSP page.<br> <%//global variable, database nameString DBNAME = "MyEclipse Derby"; //Connection Port    intnetworkserver_port=1527; //Driver class nameString derby_client_driver = "Org.apache.derby.jdbc.ClientDriver"; //assemble a Derby JDBC Driver URL with the above contentString derby_client_url= "Jdbc:derby://localhost:" +Networkserver_port+        "/" +DBNAME+ "; Create=true"; Connection Conn=NULL; Try{        //Load DriverClass.forName (derby_client_driver). newinstance (); //to establish a connection, three parameters: Url,username,passwordconn = Drivermanager.getconnection (Derby_client_url, "XR", "111111"); //Create a statement objectStatement st =conn.createstatement (); //Execute SQL statement//build a table named UserInfo, using the Execute methodSt.execute ("CREATE Table USERINFO (USERID INT not NULL, USERNAME VARCHAR (TEN) NOT null)"); //Adding and deleting the contents of the table, using the Executeupdate methodSt.executeupdate ("INSERT into USERINFO (USERID, USERNAME) VALUES (1, ' nostring ')"); St.executeupdate ("INSERT into USERINFO (USERID, USERNAME) VALUES (2, ' Bechar ')"); St.executeupdate ("INSERT into USERINFO (USERID, USERNAME) VALUES (3, ' Alexview ')"); //read the contents of the table, using the ExecuteQuery methodResultSet rs = st.executequery ("SELECT * from USERINFO"); //Loop Output Content         while(Rs.next ()) {intUserID = Rs.getint (1); String username= Rs.getstring (2); Out.println ("-------------------<br>"); Out.println ("User id:" + userID + "<br>"); Out.println ("User username:" + username + "<br>"); Out.println ("-------------------<br>"); }            }Catch(SQLException e) {out.print (E.geterrorcode ()+ "<br>"); Out.print (E.getcause ()+ "<br>"); Out.print (E.getmessage ()+ "<br>"); if(Conn! =NULL) Conn.close (); } %> </body>

Take the =-=, and try the MySQL version a few days later.

Pending Update-———————— 2017.4.6

JAVA EE Learning Note [V3 JDBC Connection Database]

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.