JDBC訪問數據庫(MySQL)示例

JDBC系列教程見鏈接


package Unit32.DataBase;


import java.sql.*;

/**
 * @program: JAVAStudy
 * @description:
 * @author: Felix
 * @create: 2020-04-03 21:16
 **/
public class SampleTest {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/javacourse?serverTimezone=GMT";
        Connection conn = null;
        String root = "root";
        String pwd = "jmm123";
        System.out.println("正在連接數據庫...");
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("Driver loaded");
            conn = DriverManager.getConnection(url, root, pwd);
            if(conn != null){
                System.out.println("數據庫連接成功");
            }
            Statement sta = conn.createStatement();
            ResultSet rs = sta.executeQuery("select * from student s");
            System.out.println("查詢到的數據如下:");
            while(rs.next()){
                String ssn = rs.getString("ssn");
                String firstName = rs.getString("firstName");
                String mi = rs.getString("mi");
                String lastName = rs.getString("lastName");
                Date birthDate = rs.getDate("birthDate");
                String street = rs.getString("street");
                String phone = rs.getString("phone");
                String zipCode = rs.getString("zipCode");
                String deptID = rs.getString("deptId");
                //ssn,firstName,mi,lastName,birthDate,street,phone,zipCode,deptID
                Student stud = new Student(ssn,firstName,mi, lastName, street, phone, zipCode, deptID, birthDate);
                System.out.println(stud);

            conn.close();
            sta.close();
            rs.close();
        } catch (ClassNotFoundException e) {
            System.out.println("數據庫驅動加載錯誤,"+e.toString());
        } catch (SQLException e) {
            System.out.println("數據庫連接錯誤,"+e.toString());
        }
    }
}
一個類實例對象–> 表裏一條記錄
student封裝
package Unit32.DataBase;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: JAVAStudy
 * @description:
 * @author: Felix
 * @create: 2020-04-03 10:35
 **/
public class Student {
    private String ssn,firstName,middleName,lastName,street,phone,zipCode,deptId;
    private Date birthDate;

    public Student(String ssn, String firstName, String middleName, String lastName, String street, String phone, String zipCode, String deptId, Date birthDate) {
        this.ssn = ssn;
        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
        this.street = street;
        this.phone = phone;
        this.zipCode = zipCode;
        this.deptId = deptId;
        this.birthDate = birthDate;
    }

    @Override
    public String toString() {
        StringBuffer buf = new StringBuffer();
        buf.append("\t" +  ssn).append(", ");
        buf.append("\t" +  firstName).append(", ");
        buf.append("\t" +  middleName).append(", ");
        buf.append("\t" +  lastName).append(", ");
        buf.append("\t" +  new SimpleDateFormat("yyyy-mm-dd").format(birthDate)).append(", ");
        buf.append("\t" +  street).append(", ");
        buf.append("\t" +  phone).append(", ");
        buf.append("\t" +  zipCode).append(", ");
        buf.append("\t" +  deptId);
        return buf.toString();
    }
    // new SimpleDateFormat(“yyyy-mm-dd”).format(birthDate))將Date變成格式化字符串,yyyy-mm-dd爲指定了格式
}//作爲示例,get set方法未實現
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章