如何解析原生Java操作MySQL數(shù)據(jù)庫過程,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新新互聯(lián),憑借十余年的網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)經(jīng)驗(yàn),本著真心·誠心服務(wù)的企業(yè)理念服務(wù)于成都中小企業(yè)設(shè)計(jì)網(wǎng)站有上千多家案例。做網(wǎng)站建設(shè),選成都創(chuàng)新互聯(lián)公司。
1.引入數(shù)據(jù)庫驅(qū)動(dòng)的jar包 以通過maven引入mysql driver為例
1.1 到http://mvnrepository.com 搜索 mysql
1.2 復(fù)制所需maven配置文件到工程的 pom.xml
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version></dependency>
2.創(chuàng)建數(shù)據(jù)庫連接類DBUtil.java用以連接與關(guān)閉數(shù)據(jù)庫
//文件名: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【數(shù)據(jù)庫地址】:3306【端口】/【數(shù)據(jù)庫名稱】?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.在java代碼中對(duì)表進(jìn)行操作
3.1 查,刪,改類似
//查找table表重的 id和nameString sql = "select id,name from table";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 增
int basicinfoID = 0;String 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 { // 關(guān)閉自動(dòng)提交 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 { // 事務(wù)回滾 conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } finally { DBUtil.closeJDBC(null, pstmt, conn); }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
分享題目:如何解析原生Java操作mysql數(shù)據(jù)庫過程
URL地址:http://jinyejixie.com/article24/jjihce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、網(wǎng)站營銷、響應(yīng)式網(wǎng)站、動(dòng)態(tài)網(wǎng)站、網(wǎng)站維護(hù)、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)