java連結mysql以及對資料庫的增,刪,改,查
阿新 • • 發佈:2018-12-24
package com.jdbcdemo; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Jdbcdemo { private static Connection con; // 連線資料庫 private static Driver dri; // 驅動 private static Statement sta; private static ResultSet res; // 結果集 public static Connection getconnection() { con = null; String url = "jdbc:mysql://localhost:3306/javademo"; // 資料庫的埠,及資料庫名稱 String user = "root"; String password = "hj1995HJ"; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, user, password); // 連線資料庫 } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } public static void query() { // 查詢資料庫 try { con = getconnection(); String sql = "select * from user"; // sql語句 sta = con.createStatement(); // 連線資料庫 res = sta.executeQuery(sql); // 執行SQL語句返回結果集 while (res.next()) { String name = res.getString("name"); int age = res.getInt("age"); int number = res.getInt("number"); System.out.println("name : " + name + " " + "age : " + age + " number: " + number); // 查詢的內容打印出來 } if (res != null) { // 關閉結果集 res.close(); } if (sta != null) { // 關閉資料庫連線 sta.close(); } if (con != null) { // 關閉連線 con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void insert() { // 插入資料庫 try { int a; con = getconnection(); String sql = "insert into user (name,age,number) values ('hongjun',22,148101)"; sta = con.createStatement(); a = sta.executeUpdate(sql); System.out.println("改變的行數為 : " + a); query(); if (res != null) { res.close(); } if (sta != null) { sta.close(); } if (con != null) { con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void delete() { // 刪除資料庫 try { int a; con = getconnection(); String sql = "delete from user where name='hongjun'"; sta = con.createStatement(); a = sta.executeUpdate(sql); System.out.println("改變的行數為 : " + a); query(); if (res != null) { res.close(); } if (sta != null) { sta.close(); } if (con != null) { con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void changdata() { // 修改資料庫 try { int a; con = getconnection(); String sql = "update user set number=100 where name='hong'"; sta = con.createStatement(); a = sta.executeUpdate(sql); System.out.println("改變的行數為 : " + a); query(); if (res != null) { res.close(); } if (sta != null) { sta.close(); } if (con != null) { con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void prequery() { // 預編譯查詢資料庫 con = getconnection(); String sql = "select * from user"; PreparedStatement pst; try { pst = con.prepareStatement(sql); res = pst.executeQuery(); while (res.next()) { String name = res.getString("name"); int age = res.getInt("age"); int number = res.getInt("number"); System.out.println("name : " + name + " " + "age : " + age + " number: " + number); } if (res != null) { res.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void preinsert() { // 預編譯插入資料庫 int a; con = getconnection(); String sql = "insert into user (name,age,number) values ('hongjun',22,148101)"; PreparedStatement pst; try { pst = con.prepareStatement(sql); a = pst.executeUpdate(); System.out.println("改變的行數為 : " + a); query(); if (res != null) { res.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void predelete() { // 預編譯刪除資料庫 int a; con = getconnection(); String sql = "delete from user where name='hongjun'"; PreparedStatement pst; try { pst = con.prepareStatement(sql); a = pst.executeUpdate(); System.out.println("改變的行數為 : " + a); query(); if (res != null) { res.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void prechangdate() { // 預編譯刪除資料庫 int a; con = getconnection(); String sql = "update user set number=100 where name='hong'"; PreparedStatement pst; try { pst = con.prepareStatement(sql); a = pst.executeUpdate(); System.out.println("改變的行數為 : " + a); query(); if (res != null) { res.close(); } if (pst != null) { pst.close(); } if (con != null) { con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { query(); // 查詢資料庫 insert(); // 插入資料庫 delete(); // 刪除資料庫資料 changdata(); // 修改資料 prequery(); // 預編譯處理查詢 preinsert(); // 預編譯插入查詢 predelete();// 預編譯刪除查詢 prechangdate(); // 預編譯處理 } }