Java串連MySQL資料庫,並進行增刪改查

來源:互聯網
上載者:User

標籤:ast   nal   ada   bool   sql   ret   ora   .exe   rest   

1、具體的代碼實現

import java.sql.*;public class DatabaseService  {/** * Create Connection *  * @param dbtype * @param username * @param password * @param url * @return * @throws Exception */public static Connection connectDBDriver(String dbtype, String username,String password, String url) throws Exception {Connection conn = null;try {if (dbtype.equals("mysql")) {Class.forName("com.mysql.jdbc.Driver").newInstance();} else if (dbtype.equals("oracle")) {Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();} else {}conn = DriverManager.getConnection(url, username, password);} catch (Exception e) {e.printStackTrace();}return conn;}/** * close DB *  * @param conn * @throws Exception */public void closeDBDriver(Connection conn) throws Exception {try {conn.close();} catch (Exception e) { /* ignore close errors */e.printStackTrace();}}/** * get ResultSet *  * @param conn * @param sql * @return * @throws Exception */private static ResultSet getResultSet(Connection conn, String sql)throws Exception {ResultSet resultSet = null;try {// PreparedStatement pstmt;// ResultSet rset;//任意的前後滾動;設定為唯讀類型的參數Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);// pstmt = conn.prepareStatement(sql);resultSet = statement.executeQuery(sql);} catch (Exception e) {e.printStackTrace();}return resultSet;}/** * get ColumnCount *  * @param resultSet * @return * @throws Exception */private static int getColumnCount(ResultSet resultSet) throws Exception {int columnCount = 0;try {// ResultSet resultSet = this.getResultSet(conn, sql);//getMetaData()  擷取此 ResultSet 對象的列的編號、類型和屬性//getColumnCount()  返回此 ResultSet 對象中的列數。columnCount = resultSet.getMetaData().getColumnCount();if (columnCount == 0) {}} catch (Exception e) {e.printStackTrace();}return columnCount;}/** * get ColumnCount *  * @param conn * @param sql * @return * @throws Exception */public static int getColumnCount(Connection conn, String sql) throws Exception {int columnCount = 0;try {// ResultSet resultSet = this.getResultSet(conn, sql);columnCount = getResultSet(conn, sql).getMetaData().getColumnCount();if (columnCount == 0) {}} catch (Exception e) {e.printStackTrace();}return columnCount;}/** * get RowCount *  * @param conn * @param sql * @return * @throws Exception */public int getRowCount(Connection conn, String sql) throws Exception {int rowCount = 0;try {ResultSet resultSet = getResultSet(conn, sql);// booleanlast()  將游標移動到此 ResultSet 對象的最後一行。resultSet.last();// booleanlast()  將游標移動到此 ResultSet 對象的最後一行。rowCount = resultSet.getRow();if (rowCount == 0) {}} catch (Exception e) {e.printStackTrace();}return rowCount;}/** * get RowCount *  * @param resultSet * @return * @throws Exception */private static int getRowCount(ResultSet resultSet) throws Exception {int rowCount = 0;try {resultSet.last();rowCount = resultSet.getRow();if (rowCount == 0) {}} catch (Exception e) {e.printStackTrace();}return rowCount;}/** * get data by row index and col index *  * @param conn * @param sql * @param row * @param col * @return * @throws Exception */public static String getData(Connection conn, String sql, int row, int col)throws Exception {String data = null;int rownum = 0;int rowcount = 0;int colcount = 0;try {ResultSet resultSet = getResultSet(conn, sql);colcount = getColumnCount(resultSet);rowcount = getRowCount(resultSet);//beforeFirst() 將游標移動到此 ResultSet 對象的開頭,正好位於第一行之前。resultSet.beforeFirst();if (rowcount > 0) {if (row <= 0 || row > rowcount) {} else {if (col <= 0 || col > colcount) {} else {while (resultSet.next()) {rownum++;if (rownum == row) {data = resultSet.getString(col);break;}}}}} else {}} catch (Exception e) {e.printStackTrace();}return data;}/** * get data by row index and col index *  * @param conn * @param sql * @param row * @param field * @return * @throws Exception */public static String getData(Connection conn, String sql, int row, String field)throws Exception {String data = null;int rownum = 0;int rowcount = 0;// int colcount = 0;try {ResultSet resultSet = getResultSet(conn, sql);// colcount = getColumnCount(resultSet);rowcount = getRowCount(resultSet);resultSet.beforeFirst();if (rowcount > 0) {if (row <= 0 || row > rowcount) {} else {while (resultSet.next()) {rownum++;if (rownum == row) {data = resultSet.getString(field);break;}}}} else {}} catch (Exception e) {e.printStackTrace();}return data;}// executeUpdate方法可以執行新增、更新、刪除三種sql語句public static int executeUpdate(Connection conn, String sql) {Statement stmt = null;try {stmt = conn.createStatement();stmt.executeUpdate(sql);int updateCount = stmt.getUpdateCount();return updateCount;} catch (Exception e) {e.printStackTrace();} finally {if (stmt != null) {try {stmt.close();} catch (SQLException e) {e.printStackTrace();}}}return 0;}}

  2、調用函數實現增刪改查

public class JdbcData {     public static void main(String[] args) {        Connection connection = databaseService.connectDBDriver("mysql","username","password","URL");        String sql = "XXXX‘";        //查詢資料,擷取到資料        String data = DatabaseService.getData(connection, sql,1,1);        String sqlstatus = "XXXX";        //update資料        DatabaseService.executeUpdate(connection,sqlstatus);     }}

  

Java串連MySQL資料庫,並進行增刪改查

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.