Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Procedure for connecting JDBC to MySQL

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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;}

}

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report