Java原生操作資料庫(不使用架構)

來源:互聯網
上載者:User

標籤:password   dex   table   java代碼   job   localhost   resultset   pid   server   

1.引入資料庫驅動的jar包 以通過maven引入mysql driver為例

  1.1 到http://mvnrepository.com 搜尋 mysql

  1.2 複製所需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.建立資料庫連接類DBUtil.java用以串連與關閉資料庫

//檔案名稱: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【資料庫地址】:3306【連接埠】/【資料庫名稱】?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代碼中對錶進行操作

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 { // 關閉自動認可 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 { // 交易回復 conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } finally { DBUtil.closeJDBC(null, pstmt, conn); }

 

Java原生操作資料庫(不使用架構)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.