Get the App
SLTechnology News&Howtos  ›  Database  › 

Procedure for connecting JDBC to MySQL

Shulou Source: shulou.com Published: 2022-06-01 04:39:19 10月31日 Update

Package testJDBC

Import java.sql.*

/ * *

@ Author Piglet @ Email zhuhuaikuan@gmail.com@Data 2019-8-28 14:27@Version V1.0

@ description JDBC query database example

, /

Public class JDBCdemo1 {

Public static void main (String [] args) {

Connection conn = null

Statement statement = null

ResultSet resultSet = null

/ / 1. Register the database driver

Try {

Class.forName ("com.mysql.jdbc.Driver"); / / load a class into memory, in which there is a static code block with registered driver code / / static code block pseudo code as follows: DriverManager.registerDriver (new Driver ()) / / 2. Get database connection String url = "jdbc:mysql://localhost:3306/" + "jt_db" + "? characterEncoding=utf-8"; / * jdbc:mysql: protocol name * localhost:3306 host address and port * jt_db database name *? characterEncoding=utf-8? + Parameter * / String user = "root"; / / database connection user name String passWord = "123456"; / / database connection password conn = DriverManager.getConnection (url, user, passWord); / / 3. Get transporter statement = conn.createStatement (); / * Statement s = con.createStatement (); * PreparedStatement p = con.preparedStatement (); * * PreparedStatement is a subclass of Statement * placeholder can be used and is precompiled, and batch processing is more efficient than Statement * * example: * String sql = "update user set password=?" Where username=? "; * preparedStatement = conn.prepareStatement (sql); * / / preparedStatement.setString (the nth question mark, the variable represented); * preparedStatement.setString (1); * preparedStatement.setString (2) username; * preparedStatement.executeUpdate (); * * / 4. Execute the sql statement and return the execution result String sql = "select * from account"; / * executeQuery (); the statement used to produce a single result set, which is used to execute the SELECT statement, and the return value is an ResultSet result set object, including the result of the sql query * executeUpdate () Used to execute INSERT, UPDATE or DELETE statements and SQL DDL (data definition language) statements, return an int value and record the number of affected record rows * / resultSet = statement.executeQuery (sql); / / 5. Processing result / * ResultSet result set object * ResultSet is the result set object returned after executing the sql statement of the query class, including the result of the sql query * a. The method to traverse the data row * next ()-moves the index pointing to the data row down one row * b. Method of obtaining data * getInt (int columnIndex) * getInt (String columnLable) * getString (int columnIndex) * getString (String columnLable) * getDouble (int columnIndex) * getDouble (String columnLable) * getObject (int columnIndex) * getObject (String columnLable) * / while (resultSet.next ()) {int id = resultSet.getInt ("id") String name = resultSet.getString ("name"); double money = resultSet.getDouble ("money"); System.out.println (id + ":" + name + ":" + money);} catch (Exception e) {e.printStackTrace ();} finally {/ / 6. Release resources / / the later you get, the first you release! / / it is best to put the release resource code in finally: if (resultSettings null) {try {resultSet.close ();} catch (SQLException e) {e.printStackTrace ();} finally {resultSet = null;}} if (statementsetting null) {try {statement.close ();} catch (SQLException e) {e.printStackTrace () } finally {statement = null;}} if {try {conn.close ();} catch (SQLException e) {e.printStackTrace ();} finally {conn = null;}

}

}

Tags: Data result code database statement query object including method resource static driver line host representative memory single parameter variable address Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information OPPO Reno Microsoft Huawei