java-JDBC (java connect to the database)

Java to connect to MySQL requires a driver package. If you need it, you can send a private message to my mysql5.1 and mysql8.0 versions. After decompression, you will get the jar library file, and then import the library file in the corresponding project.
For example (take IntelliJ IDEA as an example):

1. Click File >Project Structure

Insert picture description here

2. Click the + sign in Dependencies, find the location of the driver package, put a √ in front, and then click OK

Insert picture description here
3. Connect to the database

package sc;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;

public class DBtest {
    
    
//自己数据库的账号和密码,以及自己的端口号(一般端口号都为3306)5.5版本以下的数据库(characterEncoding = utf8&&useSSL=false&serverTimezone=UTC")不用写
    public static final String URL = "jdbc:mysql://localhost:3308/test1?characterEncoding = utf8&&useSSL=false&serverTimezone=UTC";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "root";

    public static Connection conn;
    public static Connection getConn() {
    
    

        try {
    
    
            Class.forName("com.mysql.cj.jdbc.Driver");
            try {
    
    
                conn=DriverManager.getConnection(URL, USERNAME, PASSWORD);
            } catch (SQLException e) {
    
    
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
    
    
            e.printStackTrace();
        }
        return conn;
    }

    //关闭数据库连接
    public static void closeConn(Connection conn){
    
    
        try {
    
    
            conn.close();
        } catch (SQLException e) {
    
    
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
    
    
        System.out.println(getConn());
    }
}

4. Define an interface

package sc;

import java.util.List;

import sc.DBtest;

public interface IUserInfoDao {
    
    

    //定义接口
    //查询某个人
    public UserInfo findId(int id);
    
    //查询所有人
    public List<UserInfo> findAll();


}

5. Construct a method with and without parameters

package sc;

public class UserInfo {
    
    
    private int empno;
    private String ename;
    private String job;
    private int mgr;
    private String hiredate;
    private int sql;
    private int comm;
    private int deptno;

    public int getEmpno() {
    
    
        return empno;
    }

    public void setEmpno(int empno) {
    
    
        this.empno = empno;
    }

    public String getEname() {
    
    
        return ename;
    }

    public void setEname(String ename) {
    
    
        this.ename = ename;
    }

    public String getJob() {
    
    
        return job;
    }

    public void setJob(String job) {
    
    
        this.job = job;
    }

    public int getMgr() {
    
    
        return mgr;
    }

    public void setMgr(int mgr) {
    
    
        this.mgr = mgr;
    }

    public String getHiredate() {
    
    
        return hiredate;
    }

    public void setHiredate(String hiredate) {
    
    
        this.hiredate = hiredate;
    }

    public int getSql() {
    
    
        return sql;
    }

    public void setSql(int sql) {
    
    
        this.sql = sql;
    }

    public int getComm() {
    
    
        return comm;
    }

    public void setComm(int comm) {
    
    
        this.comm = comm;
    }

    public int getDeptno() {
    
    
        return deptno;
    }

    public void setDeptno(int deptno) {
    
    
        this.deptno = deptno;
    }

    public UserInfo(int empno, String ename, String job, int mgr, String hiredate, int sql, int comm, int deptno) {
    
    
        this.empno = empno;
        this.ename = ename;
        this.job = job;
        this.mgr = mgr;
        this.hiredate = hiredate;
        this.sql = sql;
        this.comm = comm;
        this.deptno = deptno;
    }

    public UserInfo(String ename, String job, int mgr, String hiredate, int sql, int comm, int deptno) {
    
    
        this.ename = ename;
        this.job = job;
        this.mgr = mgr;
        this.hiredate = hiredate;
        this.sql = sql;
        this.comm = comm;
        this.deptno = deptno;
    }
    public UserInfo(){
    
    
        super();
    }


}

6. Interface implementation class

package sc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public  class UserInfoDaoImpl implements IUserInfoDao{
    
    


    @Override
    public UserInfo findId(int id) {
    
    

        UserInfo emp = null;
        //获得数据库连接对象
        Connection conn = DBtest.getConn();
        //sql语句
        String sql = "select * from emp where empno = ?";
        try {
    
    
            PreparedStatement pS = conn.prepareStatement(sql);
            // 4、给SQL语句占位符赋值
            pS.setInt(1,id);
            // 5、执行查询命令
            ResultSet resultSet = pS.executeQuery();
            //4.执行查询命令
            ResultSet rs =pS.executeQuery();
            //5.循环输出
            while(rs.next()){
    
    
                emp = new UserInfo();
                emp.setEmpno(rs.getInt(1));
                emp.setEname(rs.getString(2));
                emp.setJob(rs.getString(3));
                emp.setMgr(rs.getInt(4));
//                private String hiredate;
//                private int sql;
//                private int comm;
//                private int deptno;
                emp.setHiredate(rs.getString(5));
                emp.setSql(rs.getInt(6));
                emp.setComm(rs.getInt(7));
                emp.setDeptno(rs.getInt(8));

            }
        } catch (SQLException e) {
    
    
            e.printStackTrace();
        }
        return emp;
    }




    @Override
    public List<UserInfo> findAll() {
    
    
        List<UserInfo> list = new ArrayList<UserInfo>();

        //获得数据库连接对象
        Connection conn = DBtest.getConn();
        //sql语句
        String sql = "select * from emp";
        try {
    
    
            PreparedStatement ps = conn.prepareStatement(sql);
            //4.执行查询命令
            ResultSet rs =ps.executeQuery();
            //5.循环输出
            while(rs.next()){
    
    
                UserInfo emp = new UserInfo();
                emp.setEmpno(rs.getInt(1));
                emp.setEname(rs.getString(2));
                emp.setJob(rs.getString(3));
                emp.setMgr(rs.getInt(4));
                emp.setHiredate(rs.getString(5));
                emp.setSql(rs.getInt(6));
                emp.setComm(rs.getInt(7));
                emp.setDeptno(rs.getInt(8));
                list.add(emp);
            }
        } catch (SQLException e) {
    
    
            e.printStackTrace();
        }

        return list;
    }
}

7.main method

package sc;

import java.util.List;
import java.util.Scanner;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        //整体查询
        UserInfoDaoImpl eil = new UserInfoDaoImpl();
        List<UserInfo> lists = eil.findAll();
        for (UserInfo emp : lists) {
    
    
            System.out.println(emp.getEmpno()+"\t"+emp.getEname()+"\t"+emp.getJob()+"\t"+emp.getMgr()+"\t"+emp.getHiredate()+"\t"+emp.getSql()+"\t"+emp.getComm()+"\t"+emp.getDeptno());
        }

        //单个查询
        System.out.println("请输入需要查询的id:");
        Scanner scanner = new Scanner(System.in);
        int anInt = scanner.nextInt();
        UserInfo emp = eil.findId(anInt);
        if (emp!=null){
    
    
            System.out.println(emp.getEmpno()+"\t"+emp.getEname()+"\t"+emp.getJob()+"\t"+emp.getMgr()+"\t"+emp.getHiredate()+"\t"+emp.getSql()+"\t"+emp.getComm()+"\t"+emp.getDeptno());
        }

    }
}

Database table data
Insert picture description here

Results in IDEA
Insert picture description here

Guess you like

Origin blog.csdn.net/s001125/article/details/110666163