JAVA小白学习之JDBC1

初学JDBC第一天,尝试着连接数据库,还很成功,测试了一下简单的增删改查。代码如下:
有些地方还是懵逼,但大概知道什么意思,前面串连的知识点还是要多练,不然真的很容易忘。
第一轮学习进行中,加油!!!


public class User {
    private Long id;
    private String name;
    private Float score;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getScore() {
        return score;
    }

    public void setScore(Float score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", score=" + score +
                '}';
    }
    public User(){

    }
    public User(Long id,String name,Float score){
        this.id = id;
        this.name = name;
        this.score = score;
    }
}

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        //SAXReader saxReader = new SAXReader();
        save();
        //find();
        //findAll();
        //update();
        //delete();
    }
    public static void save(){
        try {
            Connection connection =  getConn();
            String sql = "insert into student(name,score) values('John',73.2) ";
            Statement statement = connection.createStatement();
            int result = statement.executeUpdate(sql);
            System.out.println(result);
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void find(){
        Connection connection = getConn();
        String sql = "select * from student where id=2";
        try {
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            User user = new User();
            if(resultSet.next())
            {
                long id = resultSet.getLong(1);
                String name = resultSet.getString(2);
                Float score = resultSet.getFloat(3);
                user.setId(id);
                user.setName(name);
                user.setScore(score);
            }
            System.out.println(user);
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void findAll() {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        connection = getConn();
        String sql = "select * from student";
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            List<User> list = new ArrayList<>();
            User user = null;
            while (resultSet.next()) {
                long id = resultSet.getLong(1);
                String name = resultSet.getString(2);
                Float score = resultSet.getFloat(3);
                user = new User(id, name, score);
                list.add(user);
            }
            for (User user1 : list) {
                System.out.println(user1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (resultSet != null) {
                    resultSet.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
    public static void update(){
        Connection connection = null;
        Statement statement =null;
        connection = getConn();
        String sql = "update student set name = 'TOM',score = '66.2' where id = 3";
        try {
            statement = connection.createStatement();
            System.out.println(statement.executeUpdate(sql));
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try{
                if(connection != null){
                    connection.close();
                }
                if(statement != null){
                    statement.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    public static void delete(){
        Connection connection = null;
        Statement statement =null;
        connection = getConn();
        String sql = "delete from student where id=3";
        try {
            statement = connection.createStatement();
            System.out.println(statement.executeUpdate(sql));
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            try{
                if(connection != null){
                    connection.close();
                }
                if(statement != null){
                    statement.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    public static Connection getConn(){
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=UTF-8";
            String user = "root";
            String password = "********";
            connection = DriverManager.getConnection(url,user,password);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值