Package COM.HUAWEI.JDBC;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import Com.huawei.utils.DBUtil;
public class TestJDBC01 {
/**
* To operate the database
*
*
* Data volume can be understood as a warehouse
*
* If you want to put things in, you need a key (database-driven)
*
* Use the key (drive) to open the warehouse (database) and store the data
* @throws SQLException
* @throws ClassNotFoundException
*
*
* Steps to operate the database
*
* 1, Load Drive
*
* 2, Get connected
*
* 3. Operation database
*
* 4. Close Resources
*
*
*/
public static void Main (string[] args) throws Exception {
/**
*
* How to get Connected connection
*
* The first type:
* Class.forName () Loads the specified driver class and generates the object to be placed in DriverManager management Com.mysql.jdbc.Driver
*
* The second type:
* Provide the Meta-inf/services/java.sql.driver file with the global qualified name of the negotiated driver in the file under the Classpath
*
*
* Steps to get connection
*
* Display or implicit loading drive to DriverManager
*
* Again by the resulting drive object to get connected according to the data provided
*
*
*
*
*
*
*
*
*/
Class.forName ("Com.mysql.jdbc.Driver");
Connection Connection = null;
About URLs in JDBC
JDBC: Database type://Address: Port/Database name
Connection = Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test", "root", "123456");
System.out.println (connection);
Testexecutequery ();
Testexecuteupdate ();
Testupdate ();
}
public static void Testexecutequery () throws SQLException, classnotfoundexception{
Class.forName ("Com.mysql.jdbc.Driver");
Get Connected
Connection Connection = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test", "root", "123456");
Get the performer of the SQL statement
Statement Statement = Connection.createstatement ();
SQL performer to execute SQL statement
How SQL statements are executed
One is that a result set will be returned primarily for queries
One is to return an affected number of rows, primarily for queries other than
ResultSet rs = statement.executequery ("SELECT * from users");
Variable result set
/*while (Rs.next ()) {
System.out.println (rs.getstring (1) + ":" +rs.getobject (2));
}*/
Rs.
System.out.println ("---------------------");
while (Rs.next ()) {
System.out.println (rs.getstring ("password") + ":" +rs.getobject ("username"));
}
Freeing Resource shutdown order
ResultSet Statement Connection
Rs.close ();
Statement.close ();
Connection.close ();
}
/**
*
*/
public static void Testexecuteupdate () throws exception{
Display load Driver
Class.forName ("Com.mysql.jdbc.Driver");
Get Connected
Connection Connection = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/test", "root", "123456");
SQL statement Performer
Statement Statement = Connection.createstatement ();
String sql = "INSERT into users (Username,password) VALUES (' Wangwu ', ' 123123456 ')";
String sql = "INSERT into users (Username,password) VALUES (' Wangwu ', ' 123123456 ')";
int num = statement.executeupdate (SQL);
SYSTEM.OUT.PRINTLN (num);
Statement.close ();
Connection.close ();
}
public static void Testupdate () throws exception{
Get Connected
Connection Connection = Dbutil.getconnection ();
Statement performers
Statement Statement = Connection.createstatement ();
String sql = "UPDATE users SET password= ' abcd123 ' WHERE username= ' Wangwu '";
int num = statement.executeupdate (SQL);
SYSTEM.OUT.PRINTLN (num);
Dbutil.close (statement,connection);
/**
* Statement will have a bug about SQL injection so it's basically not used
*/
}
}
JDBC Connection Database