1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package com.sympa.lesson01;
import java.sql.*;
public class JdbcFirstDemo { public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.cj.jdbc.Driver"); String url = "jdbc:mysql://127.0.0.1:3306/jdbcstudy?serverTimezone=UTC&userUnicode=true&characterEncoding=utf8&useSSL=false"; String username = "root"; String password = ""; Connection connection = DriverManager.getConnection(url, username, password); Statement statement = connection.createStatement();
String sql = "select * from users"; ResultSet resultSet = statement.executeQuery(sql); while(resultSet.next()){ System.out.println("id=" + resultSet.getObject("id")); System.out.println("name=" + resultSet.getObject("name")); System.out.println("password=" + resultSet.getObject("password")); System.out.println("email=" + resultSet.getObject("email")); System.out.println("birthday=" + resultSet.getObject("birthday")); } resultSet.close(); statement.close(); connection.close(); } }
|