About JDBC
Before learning JDBC very early, but not solid enough to learn, connected to MySQL is always a problem, so these two days and then the JDBC to learn again.
Definition: JDBC (Java database Connectivity,java connection) is a Java API for executing SQL statements that provides unified access to a variety of relational databases, consisting of a set of classes and interfaces written in the Java language. JDBC provides a benchmark to build more advanced tools and interfaces that enable database developers to write database applications.
The APIs contained in the JDBC library are typically used with databases for the following:
Connecting to a database
Create SQL or MySQL statements
Execute SQL or MySQL in the database
Querying to view and modify data records in a database
use of JDBC
Environment configuration
Both JDK and MySQL have to be, and the corresponding environment variables should be well matched.
Guide Package
Mysql-connector-java-5.0.8-bin.jar
Import java.sql.*; ! Do not lead into com.mysql.jdbc.*
Create a connection
/*4 configuration parameter 1. Driverclassname: com.mysql.jdbc.Driver 2. URL: jdbc:mysql://localhost:3306/mydb3 3. Username:root 4.
Create statement object send MySQL statement to implement the database additions and deletions
/ * * 1. Create a MySQL statement by Connection object Statement * >statement as the sender of the MySQL statement * 2. Call it int Excuteupdate (String sql) can send DDL DML * /Statement stmt = Con.createstatement (); String sql = "INSERT into student values (' 20166815 ', ' cui ', ' male ', 21, ' Faith Tube ')"; Just change the contents of this statement to achieve the operation of the database stmt.executeupdate (SQL);
Implementing a query to a database
public void Fun2 () throws ClassNotFoundException, sqlexception{ //Get JDBC Four configuration parameters Class.forName (" Com.mysql.jdbc.Driver "); Create the connection Connection con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/mydb3", "root", "20166815"); MySQL Query statement Statement stmt = Con.createstatement (); ResultSet rs = stmt.executequery ("SELECT * from student"); Parse resultset while (Rs.next ()) { String sno = rs.getstring (1); String sname = rs.getstring (2); String ssex = rs.getstring (3); Integer sage = rs.getint (4); String sdept = rs.getstring (5); System.out.println (sno+ "" +sname+ "" +ssex+ " " +sage+ " " +sdept); } }
Normalization of the JDBC code
The connection Statement ResultSet object that is created is closed in the order in which it was established after the program ends
public void Fun3 () {//Normalization of code
Connection con = NULL in the outer space; Statement stmt = null; ResultSet rs = null; try{class.forname ("Com.mysql.jdbc.Driver"); con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/mydb3", "root", "20166815"); stmt = Con.createstatement (); Rs=stmt.executequery ("SELECT * from student"); while (Rs.next ()) {String sno = rs.getstring (1); String sname = rs.getstring (2); String ssex = rs.getstring (3); Integer sage = rs.getint (4); String sdept = rs.getstring (5); System.out.println (sno+ "" +sname+ "" +ssex+ "" +sage+ "" +sdept "); }}catch (Exception e) {throw new RuntimeException (e); }finally{try{
First determine if three objects exist, and then close again. if (rs!=null) rs.close (); if (stmt!=null) stmt.close (); if (con!=null) con.close (); }catch (Exception e) {throw new RuntimeException (e); } } }
The above is the use of JDBC to implement database additions and deletions of the most basic operation
The first time to write a blog, the wrong place also look at the big guy correct.
A lot of attention
The basic operation of JDBC to implement database additions and deletions