您好,登錄后才能下訂單哦!
使用java連接mysql數據庫并實現增刪改查等操作?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
首先,需要把MySQL與Java連接的jar(mysql-connector-java-5.1.6-bin.jar)包導入工程.
package com.cn.edu; import java.beans.Statement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class helloworld { private Connection conn = null; PreparedStatement statement = null; // connect to MySQL void connSQL() { String url = "jdbc:mysql://localhost:3306/hello?characterEncoding=UTF-8"; String username = "root"; String password = "root"; // 加載驅動程序以連接數據庫 try { Class.forName("com.mysql.jdbc.Driver" ); conn = DriverManager.getConnection( url,username, password ); } //捕獲加載驅動程序異常 catch ( ClassNotFoundException cnfex ) { System.err.println( "裝載 JDBC/ODBC 驅動程序失敗。" ); cnfex.printStackTrace(); } //捕獲連接數據庫異常 catch ( SQLException sqlex ) { System.err.println( "無法連接數據庫" ); sqlex.printStackTrace(); } } // disconnect to MySQL void deconnSQL() { try { if (conn != null) conn.close(); } catch (Exception e) { System.out.println("關閉數據庫問題 :"); e.printStackTrace(); } } // execute selection language ResultSet selectSQL(String sql) { ResultSet rs = null; try { statement = conn.prepareStatement(sql); rs = statement.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; } // execute insertion language boolean insertSQL(String sql) { try { statement = conn.prepareStatement(sql); statement.executeUpdate(); return true; } catch (SQLException e) { System.out.println("插入數據庫時出錯:"); e.printStackTrace(); } catch (Exception e) { System.out.println("插入時出錯:"); e.printStackTrace(); } return false; } //execute delete language boolean deleteSQL(String sql) { try { statement = conn.prepareStatement(sql); statement.executeUpdate(); return true; } catch (SQLException e) { System.out.println("插入數據庫時出錯:"); e.printStackTrace(); } catch (Exception e) { System.out.println("插入時出錯:"); e.printStackTrace(); } return false; } //execute update language boolean updateSQL(String sql) { try { statement = conn.prepareStatement(sql); statement.executeUpdate(); return true; } catch (SQLException e) { System.out.println("插入數據庫時出錯:"); e.printStackTrace(); } catch (Exception e) { System.out.println("插入時出錯:"); e.printStackTrace(); } return false; } // show data in ju_users void layoutStyle2(ResultSet rs) { System.out.println("-----------------"); System.out.println("執行結果如下所示:"); System.out.println("-----------------"); System.out.println(" 用戶ID" + "/t/t" + "淘寶ID" + "/t/t" + "用戶名"+ "/t/t" + "密碼"); System.out.println("-----------------"); try { while (rs.next()) { System.out.println(rs.getInt("ju_userID") + "/t/t" + rs.getString("taobaoID") + "/t/t" + rs.getString("ju_userName") + "/t/t"+ rs.getString("ju_userPWD")); } } catch (SQLException e) { System.out.println("顯示時數據庫出錯。"); e.printStackTrace(); } catch (Exception e) { System.out.println("顯示出錯。"); e.printStackTrace(); } } public static void main(String args[]) { helloworld h = new helloworld(); h.connSQL(); String s = "select * from ju_users"; String insert = "insert into ju_users(ju_userID,TaobaoID,ju_userName,ju_userPWD) values("+8329+","+34243+",'mm','789')"; String update = "update ju_users set ju_userPWD =123 where ju_userName= 'mm'"; String delete = "delete from ju_users where ju_userName= 'mm'"; if (h.insertSQL(insert) == true) { System.out.println("insert successfully"); ResultSet resultSet = h.selectSQL(s); h.layoutStyle2(resultSet); } if (h.updateSQL(update) == true) { System.out.println("update successfully"); ResultSet resultSet = h.selectSQL(s); h.layoutStyle2(resultSet); } if (h.insertSQL(delete) == true) { System.out.println("delete successfully"); ResultSet resultSet = h.selectSQL(s); h.layoutStyle2(resultSet); } h.deconnSQL(); } }
notice:
1、現在一般用的驅動是com.mysql.jdbc.Driver
,以前的那個什么org的驅動雖然封裝了com.mysql.jdbc.Driver
,但不好用,過時了。
2、prepareStatement(sql)
是statement
的子類,比statement好用。
3、如果數據庫中定義的是int值,那么sql語句中要把int單獨提出來。如".....values("+8329+","+34243+",'mm','789')"
關于使用java連接mysql數據庫并實現增刪改查等操作問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。