In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.