Get the App
SLTechnology News&Howtos  ›  Development  › 

How to parse the process of operating mysql database with native Java

Shulou Source: shulou.com Published: 2022-06-03 06:15:54 09月22日 Update

How to parse the native Java operation mysql database process, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

1. Introduction of database-driven jar package taking the introduction of mysql driver through maven as an example

1.1 search for mysql from http://mvnrepository.com

1.2 copy the required maven configuration file to the pom.xml of the project

Mysql mysql-connector-java 6.0.6

two。 Create a database connection class DBUtil.java to connect and close the database

/ File name: DBUtil.javaimport java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class DBUtil {static String user = "root"; static String password = "root"; static String url = "jdbc:mysql://localhost [database address]: 3306 [port] / [database name]? serverTimezone=UTC"; static {try {Class.forName ("com.mysql.cj.jdbc.Driver") } catch (ClassNotFoundException e) {e.printStackTrace ();}} public static Connection getConnection () {Connection conn = null; try {conn = DriverManager.getConnection (url, user, password);} catch (SQLException e) {e.printStackTrace ();} return conn;} public static void closeJDBC (ResultSet rs, Statement stmt, Connection conn) {if (rs! = null) {try {rs.close ();} catch (SQLException e) {e.printStackTrace () }} if (stmt! = null) {try {stmt.close ();} catch (SQLException e) {e.printStackTrace ();}} if (conn! = null) {try {conn.close ();} catch (SQLException e) {e.printStackTrace ();}}

3. Manipulate the table in java code

3.1 check, delete, change similar

/ find id and nameString sql = "select id,name from table" of table table weight; Connection conn = DBUtil.getConnection (); PreparedStatement pstmt = null;try {conn.setAutoCommit (false); pstmt = conn.prepareStatement (sql); ResultSet rs = pstmt.executeQuery (); json = ResultSetToJson.ResultSetToJsonArray (rs);} catch (SQLException e) {try {conn.rollback ();} catch (SQLException E1) {e1.printStackTrace ();}} finally {DBUtil.closeJDBC (null, pstmt, conn);} return json

3.1 increase

Int basicinfoID = 0String sql = "INSERT INTO tb_resume_basicinfo (" + "basicinfo_id, realname, gender, birthday, current_loc," + "resident_loc, telephone, email, job_intension, job_experience, head_shot,applicant_id)" + "VALUES (SEQ_ITOFFER_RESUMEBASICINFO.NEXTVAL,?)"; Connection conn = DBUtil.getConnection (); PreparedStatement pstmt = null Try {/ / turn off autocommit conn.setAutoCommit (false); pstmt = conn.prepareStatement (sql); pstmt.setString (1, basicinfo.getRealName ()); pstmt.setString (2, basicinfo.getGender ()); pstmt.setTimestamp (3, basicinfo.getBirthday () = = null? Null: new Timestamp (basicinfo.getBirthday (). GetTime ()); pstmt.setString (4, basicinfo.getCurrentLoc ()); pstmt.setString (5, basicinfo.getResidentLoc ()); pstmt.setString (6, basicinfo.getTelephone ()); pstmt.setString (7, basicinfo.getEmail ()); pstmt.setString (8, basicinfo.getJobIntension ()); pstmt.setString (9, basicinfo.getJobExperience ()); pstmt.setString (10, basicinfo.getHeadShot ()); pstmt.setInt (11, applicantID); pstmt.executeUpdate () } catch (SQLException e) {try {/ / transaction rollback conn.rollback ();} catch (SQLException E1) {e1.printStackTrace ();} e.printStackTrace ();} finally {DBUtil.closeJDBC (null, pstmt, conn);}

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Data database file help process clarity transaction code content name address for this project file name article novice more for knowledge port Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi Shulou Technology Docker NVidia