环境信息:
Windows10 X64
Mysql5.7.26
JDK14
mysql-connector-java5.1.47

直接上代码,一看就废

package Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Test1 {
    public static void main(String[] args) throws Exception {
        // 加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 链接url
        String mysqlUrl = "jdbc:mysql://localhost:3306/jdbcstudy?characterEncoding=utf-8&useSSL=true&useUnicode=true";
        // 数据库用户名
        String userName = "root";
        // 数据库密码
        String userPasswd = "root";
        // 连接
        Connection connection = DriverManager.getConnection(mysqlUrl, userName, userPasswd);

        // 执行sql 查询就query 增删改就update
        Statement statement = connection.createStatement();
        String sql1 = "select * from users";
        ResultSet resultSet = statement.executeQuery(sql1);

        while (resultSet.next()){
            System.out.print("id = " + resultSet.getInt("id") + "  ");
            System.out.print("name = " + resultSet.getString("name")+ "  ");
            System.out.print("password = " + resultSet.getString("password")+ "  ");
            System.out.print("email = " + resultSet.getString("email")+ "  ");
            System.out.print("birthday = " + resultSet.getString("birthday")+ "  ");
            System.out.println();
        }

        // 关闭链接
        resultSet.close();
        statement.close();
        connection.close();
    }
}
# 执行结果:
id = 1  name = zhansan  password = 123456  email = [email protected]  birthday = 1980-12-04  
id = 2  name = lisi  password = 123456  email = [email protected]  birthday = 1981-12-04  
id = 3  name = wangwu  password = 123456  email = [email protected]  birthday = 1979-12-04

2333333.PNG

最后编辑:2020年09月21日 ©著作权归作者所有

发表评论