Java 连接 MySQL 需要驱动包,如果需要的话可以私信我mysql5.1,mysql8.0版本都有,解压后得到 jar 库文件,然后在对应的项目中导入该库文件。
例如(以IntelliJ IDEA为例):
1. 点击File >Project Structure
2.点击Dependencies中的+号,找到驱动包的位置,前面打上√然后点击OK
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.定义一个接口
package sc;
import java.util.List;
import sc.DBtest;
public interface IUserInfoDao {
//定义接口
//查询某个人
public UserInfo findId(int id);
//查询所有人
public List<UserInfo> findAll();
}
5.构建有参,无参方法
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.接口实现类
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方法
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());
}
}
}