JDBC Definition: JDBC is a type of interface defined by Sun formula that can be used by companies such as mysql,oracle to implement interfaces.
Need to import the MySQL jar package
Steps to implement JDBC:
1. Registering the database driver
2. Get a database connection (think of him as a freeway)
3. Get the Transmitter object (think of him as a motorway-driven car)
4. Transferring SQL statements to database parcels using a transmitter to obtain a result set
5. Traverse the result set
6. Close the resource (first created after close, then created first closed)
Case one:
To create a database in the database:
Create Database Day10;
Use DAY10;
CREATE TABLE User (
ID int primary key auto_increment;
Gender bit;
Name varchar (40),
Date Date
);
JDBCDemo1:
Package Cn.itheima.jdbc;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.ResultSet;import Java.sql.sqlexception;import Java.sql.statement;import Com.mysql.jdbc.driver;public class JDBCDemo1 {public static void Main (string[] args) throws Exception {//1. Registered driver (actually registered here two times) Drivermanager.registerdriver (New Driver ());//2. Gets the connection to the database connection con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/day10", "root", "169500");//3. Gets the Transfer object statement statement = Con.createstatement ();//4. Get result set ResultSet rs = Statement.executequery ("SELECT * from User") ;//5. Traverse the result set while (Rs.next ()) {String name = rs.getstring ("name"); byte gender = Rs.getbyte ("gender"); System.out.println (name+ ":" +gender);} 6. Close resource Rs.close (); Statement.close (); Con.close ();}}
Disadvantages:
In fact, a total of two registered drivers, you can see driver source registration Once, we registered a manual.
It is unreasonable to close resources.
JDBCDemo2:
package cn.itheima.jdbc;import java.sql.connection;import Java.sql.DriverManager; Import Java.sql.resultset;import Java.sql.sqlexception;import Java.sql.statement;public class JDBCDemo2 {public static void Main (string[] args) {Connection con=null; Statement Sta=null; ResultSet rs=null;try{//1. Register the driver Class.forName ("Com.mysql.jdbc.Driver");//2. Get the connection con= drivermanager.getconnection (" Jdbc:mysql://localhost:3306/day10 "," root "," 169500 ");//3. Gets the Transport object Sta=con.createstatement ();//4. Result set rs= Sta.executequery ("SELECT * from user");//5. Traversal result set while (Rs.next ()) {String name=rs.getstring ("name"); SYSTEM.OUT.PRINTLN (name);}} catch (Exception e) {e.getmessage ();} Finally{if (rs!=null) {try {rs.close ();} catch (SQLException e) {e.printstacktrace ();} Finally{rs=null;}} if (sta!=null) {try {sta.close ();} catch (SQLException e) {e.printstacktrace ();} Finally{sta=null;}} if (con!=null) {try {con.close ();} catch (SQLException e) {e.printstacktrace ();} Finally{con=null;}}}}
Cons: Code redundancy, so last write a tool class.
Operation Result:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Black Horse day10 JDBC Primer &mysql