當(dāng)前位置:首頁 > IT技術(shù) > 數(shù)據(jù)庫 > 正文

Java JDBC鏈接MySQL數(shù)據(jù)庫
2021-09-08 11:54:13

import java.sql.* ;  

public class App {
    public static void main(String[] args) throws Exception {
        System.out.println("Hello, World!");
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = null;
        Statement stmt = null;

        String url="jdbc:mysql://數(shù)據(jù)庫IP/數(shù)據(jù)庫名?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
        String user="root";
        String password="123456";
        conn = DriverManager.getConnection(url,user,password);

        stmt = conn.createStatement();

        // String SQL = "INSERT INTO test1 VALUES(10001,老李,男,35)";//// String SQL = "delete from test1 where id=10001";//// String SQL = "update test1 set (id=10010,老六,女,66) where id=10001";//// stmt.executeUpdate(SQL);

        String SQL = "select * from test1";//
        ResultSet rs = stmt.executeQuery(SQL);
        while(rs.next()){
            int id  = rs.getInt("id");//錄入
            String name  = rs.getString("name");
            String sex  = rs.getString("sex");
            int age  = rs.getInt("age");
            System.out.print("id: " + id+"	");//顯示
            System.out.print("name: " + name+"	");
            System.out.print("sex: " + sex+"	");
            System.out.println("age: " + age+"	");
         }
        rs.close();


        stmt.close();
        conn.close();
    }

}

?

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務(wù)立即開通 >