【java】用java語言通過JDBC連接實現對數據庫的增刪改查

mysql 密碼重置:  https://jingyan.baidu.com/article/ff411625efbd2012e4823730.html

整體項目截圖:

1.創建數據庫runoob

2.創建表websites

3.填入數據

4.Eclipse導入jar包

jar包下載鏈接:http://static.runoob.com/download/mysql-connector-java-5.1.39-bin.jar

導入jar包的方法:https://jingyan.baidu.com/article/ca41422fc76c4a1eae99ed9f.html

5.編寫JDBC連接數據庫的程序MySQLDemo.java

package my;

import java.sql.*;
 
public class MySQLDemo {
 
    // MySQL 8.0 以下版本 - JDBC 驅動名及數據庫 URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB";
 
    // MySQL 8.0 以上版本 - JDBC 驅動名及數據庫 URL
    //static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";  
    //static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&serverTimezone=UTC";
 
 
    // 數據庫的用戶名與密碼,需要根據自己的設置
    static final String USER = "root";
    static final String PASS = "123456";
 
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try{
            // 註冊 JDBC 驅動
            Class.forName(JDBC_DRIVER);
        
            // 打開鏈接
            System.out.println("連接數據庫...");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);
        
            // 執行查詢
            System.out.println(" 實例化Statement對象...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT id, name, url FROM websites";
            ResultSet rs = stmt.executeQuery(sql);
        
            // 展開結果集數據庫
            while(rs.next()){
                // 通過字段檢索
                int id  = rs.getInt("id");
                String name = rs.getString("name");
                String url = rs.getString("url");
    
                // 輸出數據
                System.out.print("ID: " + id);
                System.out.print(", 站點名稱: " + name);
                System.out.print(", 站點 URL: " + url);
                System.out.print("\n");
            }
            // 完成後關閉
            rs.close();
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 處理 JDBC 錯誤
            se.printStackTrace();
        }catch(Exception e){
            // 處理 Class.forName 錯誤
            e.printStackTrace();
        }finally{
            // 關閉資源
            try{
                if(stmt!=null) stmt.close();
            }catch(SQLException se2){
            }// 什麼都不做
            try{
                if(conn!=null) conn.close();
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        System.out.println("成功連接數據庫!");
    }
}

6.增加數據

package my;

import java.sql.*;


public class Increase {

    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");//加載驅動
        
        String jdbc="jdbc:mysql://localhost:3306/runoob?useUnicode=true&characterEncoding=gbk";
        Connection conn=DriverManager.getConnection(jdbc, "root", "");//鏈接到數據庫
        
        Statement state=conn.createStatement();   //容器
        String sql="insert into websites values('8','百度','www.baidu.com','25','')";   //SQL語句
        state.executeUpdate(sql);         //將sql語句上傳至數據庫執行
        
        conn.close();//關閉通道

    }
}

7.刪除數據

package my;

import java.sql.*;


public class Delete {

    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");//加載驅動
        
        String jdbc="jdbc:mysql://localhost:3306/runoob?useUnicode=true&characterEncoding=gbk";
        Connection conn=DriverManager.getConnection(jdbc, "root", "");//鏈接到數據庫
        
        Statement state=conn.createStatement();   //容器
        String sql="delete from websites where id=3";   //SQL語句
        state.executeUpdate(sql);         //將sql語句上傳至數據庫執行
        
        conn.close();//關閉通道

    }

}

8.更改數據

package my;

import java.sql.*;

public class Update {

    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");//加載驅動
        
        String jdbc="jdbc:mysql://localhost:3306/runoob?useUnicode=true&characterEncoding=gbk";
        Connection conn=DriverManager.getConnection(jdbc, "root", "");//鏈接到數據庫
        
        Statement state=conn.createStatement();   //容器
        String sql="update websites set name='京東' where name='百度' ";   //SQL語句
        state.executeUpdate(sql);         //將sql語句上傳至數據庫執行
        
        conn.close();//關閉通道

    }

}

9.查詢數據:

package my;

import java.sql.*;

import javax.xml.stream.events.StartElement;

public class Query
{

	public static void main(String[] args) throws Exception {
        //導入驅動包
        Class.forName("com.mysql.jdbc.Driver"); 
        //鏈接至數據庫
        String jdbc="jdbc:mysql://localhost:3306/runoob?useUnicode=true&characterEncoding=gbk";
    Connection conn=DriverManager.getConnection(jdbc, "root", "");
    
    Statement state=conn.createStatement();//容器
    String sql="select * from websites";           //sql語句
    ResultSet rs=state.executeQuery(sql);     //將sql語句傳至數據庫,返回的值爲一個字符集用一個變量接收 
    
    while(rs.next()){    //next()獲取裏面的內容
    System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
                         //getString(n)獲取第n列的內容
                            //數據庫中的列數是從1開始的
    }
    
    
    
    conn.close();
    }
}

實驗結果

1.連接數據庫實驗結果截圖

2.增加數據後表的變化截圖

3.刪除id=3的數據表的變化截圖

4.將name=‘百度’更新爲‘京東’

5.查詢表中所有數據

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章