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 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;
    }
}

你可能感兴趣的:(JAVA小白学习之JDBC1)