這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Java怎么操作MySQL數(shù)據(jù)庫(kù),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)建站是一家專注于成都網(wǎng)站建設(shè)、做網(wǎng)站與策劃設(shè)計(jì),南開(kāi)網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:南開(kāi)等地區(qū)。南開(kāi)做網(wǎng)站價(jià)格咨詢:18982081108
1.引入數(shù)據(jù)庫(kù)驅(qū)動(dòng)的jar包 以通過(guò)maven引入mysql driver為例
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ù)庫(kù)連接類DBUtil.java用以連接與關(guān)閉數(shù)據(jù)庫(kù)
//文件名:DBUtil.java import 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ù)庫(kù)地址】:3306【端口】/【數(shù)據(jù)庫(kù)名稱】?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和name String 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); }
上述就是小編為大家分享的Java怎么操作mysql數(shù)據(jù)庫(kù)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站標(biāo)題:Java怎么操作mysql數(shù)據(jù)庫(kù)
路徑分享:http://jinyejixie.com/article18/pdcedp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、虛擬主機(jī)、網(wǎng)站內(nèi)鏈、定制網(wǎng)站
聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)