当前位置:首页 > IT技术 > 数据库 > 正文

Java JDBC链接MySQL数据库
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://数据库IP/数据库名?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/

开通会员,享受整站包年服务立即开通 >