JDBC API allows users to access any form of table data, especially the data stored in the relationship database.
Execution process:
• Connect the data source, such as: database.
• Database pass inquiries and update instructions.
• The results of the database response and return.
JDBC architecture
Divided into a double -layer architecture and a three -layer architecture.
Double -layer
Function: In this architecture, Java Applet or app directly access the data source.
Condition: Driver is required to interact with the database accessible.
Mechanism: The user command is passed on to the database or other data sources, and the result is returned.
Deployment: The data source can be connected through the network on another machine, called C/S configuration (can be an internal network or the Internet).
Three -layer
The special feature of the side architecture is that the intermediate service is introduced.
Process: commands and structures will pass through this layer.
Attraction: It can increase the access control of corporate data and the update of various types; in addition, it can also simplify the deployment of the application and has performance advantages in most cases.
Historical trend: In the past, due to performance problems, the middle layer was written in C or C ++. With the optimization compiler (converting Java bytecode into high -efficiency specific machine code) and the development of technology, such as EJB, Java began to be used in the middle for intermediate The development of the layer has also made the advantages of Java highlighting. Using Java as a server code language, JDBC is paid attention to.
JDBC programming steps
Load the driver:
Class.Forname (DriverClass) // Load mysql driver Class.Forname ("com.mysql.jdbc. Driver") // Load Oracle Drive Class.Forname ("Oracle.jdbc.oraCledriver" CureGet the database connection:
Copy code code as follows:
DriverManager.GetConnection ("JDBC: MySQL: //127.0.0.1: 3306/Imooc", "root", "root");
Create the statement/preparedStatement object:
Copy code code as follows:
conn.createStatement ();
const.preparestatement (SQL);
Complete instance
Import java.sql.connection; Import Java.SQL.DriverManager; Import Java.Sql.ResultSet; Import java.sql.Statement; Public Class dbutil {Public ic final String URL = "jdbc:mysql://localhost:3306/imooc "; Public Static Final String User =" Liulx "; Public Static Final String Password =" 123456 "; Public Static Void Main (String [] ARGS) Throws Exception { / / / /1. Load the driver Class.Forname ("com.mysql .jdbc.driver "); // 2. Get the database connection connection conn = DriverManager.getConnection (url, user, password); // 3. Operate the database to achieve additional, deletion, check the statement stmt = Conn.CreateStatemet ( ); ResultSet RS = Stmt.executequery ("Select user_name, Age from ImoC_GODDESS"); // If there is data, RS.NEXT () Return to True While (RS.NEXT ())) {System.out.println (RS.GetStringstring ("user_name" )+"Age:"+RS.getint ("Age");}}} Addition, deletion and change
Public Class DBUTIL {Public Static Final String url = "JDBC: MySQL: // LOCALHOST: 3306/ImoOC"; Atic final string password = "123456"; Private Static Connection Conn = Null ; static {try {// 1. Load the driver class.Forname ("com.mysql.jdbc.driver"); // 2. Get database connection conn = driverManager.getConnection (url, user, password);} CH (CH (CH ( ClassNotFoundexception E) {e.printstacktrace ();} Catch (sqlexception E) {e.printstacktrace ();}} Public Static Connection () {) turn conn;}} // model package liulx.model; Import java.util. Date; Public Class Goddess {Private Integer ID; Private String User_name; Private Integer Sex; Private Integer Age; VA.UTIL.DATE PRIVATE String Email; Private String Mobile; Private String Create_user; Private String update_user; Private date create_date; Private date update_date; private integer isdel; // getter setter method. Essence Essence }//---------dao层--------------package liulx.dao;import liulx.db.DbUtil;import liulx.model.Goddess;import java. SQL.Connection; Import Java.sql.ResultSet; Import Java.sql.SQL.SQLEXCeption; Import Java.Sql.Statement; Import Java.util.arrayList; Import Java.util .List; Public Class Goddesdao {// Add Public Void AddgodDess ( GodDess G) Throws Sqlexception {// Get connect connection conn = dbutil.getConnection (); // Sql string sql = "Insert IMOOC_GODDESS , BIRTHDAY, Email, Mobile, "+" Create_user, Create_date, Update_user , Update_date, isdel) " +" Values (" +"?,?,?,?,?,?,?, Current_date (),?, Current_date (),?) "; Preparestatement (SQL); // Pre -compilation SQL to reduce SQL execution // Passing PTMT.SetString (1, G.Getuser_name ()); ptmt.Setint (2, g.getsex ()); ptmt.Setint (3, 3, 3, 3, g.getage ()); ptmt.setdate (4, new date (g.getbirthDay (). Gettime ())); ptmt.SetString (5, G.Getemail ())); ()); Ptmt.SetString (7, G.Getcreate_user ()); ptmt.setString (8, G.Getupdate_user ()); ptmt.Setint (9, G.Getisdel ()); // Permt.execute ( );} Public Void UpdategodDess () {// Get connect connection conn = dbutil.getConnection (); // SQL, Each row adds empty sql = "Update ImoC_GODESS" + "SET US er_name = ?, sex = ?, Age = ?, BIRTHDAY =?, Email =?, Mobile = ?, "+" Update_user = ?, Update_date = Current_date (), isdel =? "+" where id =? "; .Preparestatement ( sql); // Pre -compile SQL to reduce SQL execution // Passing PTMT.SetString (1, G.Getuser_name ()); ptmt.Setint (2, g.getsex ()); ptmt.Setint (3, G. getage ()); ptmt.Setdate );; 7, G.Getupdate_user ()); ptmt.Setint (8, G.Getisdel ()); ptmt.Setint (9, G.Getid ()); // Phase ptmt.execute (); } PUBLIC VOID DELGODDESS () {// Get connect connection conn = dbutil.GetConnection (); // SQL, Each row adds an air. Compile SQL to reduce SQL execution PreparedStatement ptmt = conn.preparestatement (SQL); // Pass PTMT.Setint (1, ID); // Phase ptmt.execute ();} Public list <GODDESS> Query () Throws {Connection conn = dbutil.getConnection (); Statement stmt = conn.createStatement (); ResultSet RS = Stmt.executequry ("Select user_name, Age FROM IMOOC_GODDESS"); rrayList<Goddess>(); Goddess g = null; while( rs.next ()) {g = new goddess (); g.setuser_name (RS.GetString ("user_name"); g.setage (RS.Getint ("Age"); gs.add (g);} Return gs;} Public Goddess get () {goddess g = null; // Get connecting connection continuity. OC_GODESS WHERE ID =? ";; // Pre -compile SQL to reduce SQL execution prepareDStatement ptmt = conn.preparestatement (SQL); // Passing PTMT.Setint (1, ID); // execute resultSet RS = ptmt.executequery (); while (RS.N EXT (EXT )) {g = new goddess (); g.Setid (RS.Getint ("ID"); g.setuser_name (RS.GetString ("User_name"); g.Setage (RS.getint ("Age") ); SeetSex (RS.getint ("Sex"); g.SetbirthDay (RS.GetDate ("BIRTHDAY"); G.Setemail (RS.GetString ("Email"); g.Setmobile (RS. GetString ("Mobile"); g.setcreate_date (RS.GetDate ("Create_date"); g.setcreate_user (RS.getString ("Create_user"); pdate_date ")); g.setupdate_user (RS.GetString ("Update_user"); g.Setisdel (RS.Getint ("ISDEL");} Return g;}}