JDBC访问数据库(MySQL)示例

6 篇文章 0 订阅
5 篇文章 0 订阅

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方法未实现
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值