溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何解析原生Java操作mysql數(shù)據(jù)庫過程

發(fā)布時間:2021-10-13 10:26:01 來源:億速云 閱讀:132 作者:柒染 欄目:編程語言

如何解析原生Java操作mysql數(shù)據(jù)庫過程,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

1.引入數(shù)據(jù)庫驅(qū)動的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代碼中對表進(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)閉自動提交   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)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI