JDBC connection MySQL database (a) - a basic database connection

JDBC concepts
before our first look at the concept of JDBC, JDBC stands for database connectivity (Java Database Connectivity), which is a set of API, the application program for executing SQL statements can connect to relational database through this API and use SQL statements to complete the database query, add, change, delete, etc. ..
Internal database handles different types of data are different, if to establish direct access to the data by the method of database vendors, application portability will become very poor, if a user is used in the current program is a MySQL database into Oracle's database so if need to change a lot of code with JDBC such a situation does not exist, because JDBC requires database vendors in accordance with the requirements to provide a unified database driven, which makes the code more versatile.

 

Seen from the figure, JDBC between the application and the database in the role of a bridge, with various different drive data connection, the corresponding connection operation

Learn the JDBC API
Driver Interface
Driver Interface is all JDBC drivers must implement, dedicated to database vendors use the interface. When writing JDBC program, you must put all database drivers or libraries loaded into the classpath.

DriverManager Interface
Methods role
regsiterDriver (Driver driver) for registering the JDBC driver DriverManager
getConnection (String url, String user, String password) to establish a connection to the database
Connection interface
methods role
createStatement () object creation statement sent to the SQL statement database
prepareStatement () to create preparestatement object to send SQL statements to the database
prepareCall () to create a CallableStatement object database stored procedure calls
Note: here we see createStatement create objects and methods are prepareStatement sending SQL statements to the database, but What is the difference? Let us use createStatement method, prepareStatement method after we talk about its use.

Statement interface
method role
execute (String sql) execute SQL statement returns a boolean value of true if there is indicated as a result, the query results by the method ResultSet
insert delete update method performed executeUpdate (String sql) SQL statement returns a value of type int
executeQuery performed SQL select statement
ResultSet interface
methods described
getString (String name) Gets the value of type String, name field representative name
getInt (int index) Gets an int, index representative of the field names
next () the current position to the next line
previous ( ) on the ResultSet line
designated position absolute (int row) ResultSet is
to establish a connection step
in the need to write code MySQL gas into the jar package project found MySQL \ connector J mysql-connector- java-8.0.14 in 8.0 .jar file into the project

To establish a connection to the database, we must first registration drive MySQL database
complete database driver registration, establish a connection to the database
to create a statement object
executed sql statement
traversal query the data
release resources
JDBC code base
to explain the steps to establish a connection to the database and a common API, then take a look at how to achieve the most basic of code:


the java.sql.Connection Import;
Import the java.sql.DriverManager;
Import the java.sql.ResultSet;
Import java.sql.SQLException;
Import the java.sql.Statement;
public class pracjdbc {
public static void main (String [] args) {
{the try
// registration drive. 1.
DriverManager.registerDriver (new new com.mysql.cj.jdbc.Driver ());
. / 2 * establish a connection to the database is sequentially acquisition parameters url, user, password url connect the local database, user user name, password database password * /
Connection conn =
DriverManager.getConnection ( "jdbc: MySQL:? // localhost / studentinformation serverTimezone = GMT% 2B8", "root", "81,604,152");
. 3 // create a statement object
statement st conn.createStatement = ();
String sql = "info from the SELECT *";
. // 4 execute sql statement
ResultSet rs = st.executeQuery(sql);
//5.遍历查询数据
while(rs.next()) {
int id =rs.getInt("id");
String name = rs.getString("name");
System.out.println(id+":"+name);
}
//6.释放资源
conn.close();
st.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
JDBC code is improved
just wrote most of the code is now into this we look at on the basis of just a little to make some improvements

Resource release
the resource release is not the case where the initial writing, by using the actual finally block
the resource release
the try {
IF (Conn = null!) {
Conn.Close ();
}
} the catch (SQLException E) {

e.printStackTrace ();
} {the finally
Conn = null;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
write too long, uniform write complete this operation takes a class


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCutils {
public static void ReleaseAll(Connection conn, ResultSet rs, Statement st) {
CloseConn(conn);
CloseSt(st);
CloseRs(rs);
}
public static void ReleaseAll(Connection conn, Statement st) {
CloseConn(conn);
CloseSt(st);
}
private static void CloseConn(Connection conn) {
try {
if(conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
conn = null;
}
}
private static void CloseRs(ResultSet rs) {
try {
if(rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
rs = null;
}

}
private static void CloseSt(Statement st) {
try {
if(st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
st = null;
}

}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
38 is
39
40
41 is
42 is
43 is
44 is
45
46 is
47
48
49
50
51 is
52 is
look at the steps to build into the bottom of the drive source


{static
the try {
java.sql.DriverManager.registerDriver (new new Driver ());
} the catch (SQLException E) {
the throw a RuntimeException new new ( "Can Not Register Driver!");
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
Driver in fact, we have established a drive to establish their own driving is already redundant,
modify the code as follows


The Class.forName ( "com.mysql.cj.jdbc.Driver");
. 1
2
complete code as follows:


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class pracjdbc {
public static void main(String [] args) {
Connection conn= null;
Statement st = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
conn= DriverManager.getConnection("jdbc:mysql://localhost/studentinformation?serverTimezone=GMT%2B8", "root", "81604152");
st=conn.createStatement();
String sql = "select * from info";
rs = st.executeQuery(sql);
while(rs.next()) {
int id =rs.getInt("id");
String name = rs.getString("name");
System.out.println(id+":"+name);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCutils.ReleaseAll(conn, rs, st);
}
}
}
--------------------- 

Guess you like

Origin www.cnblogs.com/ly570/p/10987478.html