目录
1.新建Web Project 项目,建立Util工具包,新建JDBCUtil类,导入jdbc-connector-java包
3.右键JDBC类->运行方式->Java应用程序,测试是否连接成功
6.新建DaoImp层,创建UserDaoImp类,实现UserDao接口方法
工具:MyEclipse 2017 CI
JDBC驱动下载:链接:
https://pan.baidu.com/s/1I90tu8pO_0lnx996k4_S0A
密码:pdax
项目源码:链接:
https://pan.baidu.com/s/1YFKtQc6REI7IWiyNXFzjmA
密码:c6oy
1.新建Web Project 项目,建立Util工具包,新建JDBCUtil类,导入jdbc-connector-java包
2.编写JDBCUtil类的相关内容
package com.ly.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class JDBCUtil {
public static Connection getConnection(){
try {
//反射的技术将驱动加进来
Class.forName("com.mysql.jdbc.Driver");
//第二步:
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/shop?characterEncoding=utf-8&useSSL=true", "root", "root");
System.out.println("数据库连接成功!");
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
*
* @param args
* 关闭相关的资源
*
*/
public static void closeAll(ResultSet rs,PreparedStatement pstm,Connection conn){
try{
if(rs!=null){rs.close();}
if(pstm!=null){pstm.close();}
if(conn!=null){conn.close();}
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
getConnection();
}
}
3.右键JDBC类->运行方式->Java应用程序,测试是否连接成功
有人可能会问,这不是WebProject吗?怎么可以用Java应用程序方式来运行,这是因为在JDBCUtil中加了main函数。加了main函数的类是可以直接用Java应用程序方式来运行的。这也可以当作测试来用,当然最好是用Java的单元测试来验证是否连接成功。
4.新建entity实体包,编写对应的实体类
一般情况下都是一张数据表对应一个实体类。
先看一下数据库中的user表:
编写实体类:
package com.ly.entity;
/***
*
* @author HDLB601T1
* 封装用户数据
*/
public class User {
private int id;
private String username;
private String pwd;
private int status;
private int role;
public User() {
super();
}
public User(int id, String username, String pwd, int status, int role) {
super();
this.id = id;
this.username = username;
this.pwd = pwd;
this.status = status;
this.role = role;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public int getRole() {
return role;
}
public void setRole(int role) {
this.role = role;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", pwd=" + pwd + ", status=" + status + ", role=" + role
+ "]";
}
}
此时的项目结构:
5.新建Dao层,创建UserDao接口
package com.ly.dao;
import java.util.List;
import com.ly.entity.User;
public interface UserDao {
/**
* 插入一条数据:
* 返回值:int
* 参数:User
* insert into user values(7,'qq','123',1,2);
*/
public int insertUser(User u);
/**
*
* @param id
* @return
* 删除用户
*/
public int deleteUser(int id );
/***
*
* @param u
* @return
* 修改用户
*
*/
public int updateUser(User u);
/**
*
*根据页码进行查询
*返回值:
*参数:page ,index
*
*/
public List<User> listByPage(int index,int pageSize);
/***
*登录的业务
*查找:
*select * from user where user=? and pwd=?
*参数:
*返回值:
*/
public User findUserByNamePwd(String username,String pwd);
public User findUserById(int id);
}
6.新建DaoImp层,创建UserDaoImp类,实现UserDao接口方法
package com.ly.daoimp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.ly.dao.UserDao;
import com.ly.entity.User;
import com.ly.util.JDBCUtil;
public class UserDaoImp implements UserDao {
@Override
public int insertUser(User u) {
// 连接数据库
Connection conn = JDBCUtil.getConnection();
PreparedStatement pstm = null;
// 创建一个查询窗口
try {
String sql = "insert into user values(?,?,?,?,?)";
pstm = conn.prepareStatement(sql);
// 需要设置真正的值insert into user values(7,'qq','123',1,2);
pstm.setInt(1, u.getId());
pstm.setString(2, u.getUsername());
pstm.setString(3, u.getPwd());
pstm.setInt(4, u.getStatus());
pstm.setInt(5, u.getRole());
// 执行sql
int num = pstm.executeUpdate();
return num;
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtil.closeAll(null, pstm, conn);
}
return 0;
}
@Override
public int deleteUser(int id) {
Connection connection = JDBCUtil.getConnection();
PreparedStatement pstm = null;
try {
String sqlString = "delete from user where id=?";
pstm = connection.prepareStatement(sqlString);
pstm.setInt(1, id);
int num = pstm.executeUpdate();
return num;
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtil.closeAll(null, pstm, connection);
}
return 0;
}
@Override
public int updateUser(User u) {
Connection connection=JDBCUtil.getConnection();
PreparedStatement pstm=null;
try {
String sqlString="update user set user=?,pwd=? ,status=?, role=? where id=? ";
pstm=connection.prepareStatement(sqlString);
pstm.setString(1, u.getUsername());
pstm.setString(2, u.getPwd());
pstm.setInt(3, u.getStatus());
pstm.setInt(4, u.getRole());
pstm.setInt(5, u.getId());
int num=pstm.executeUpdate();
return num;
} catch (SQLException e) {
e.printStackTrace();
}
finally {
JDBCUtil.closeAll(null, pstm, connection);
}
return 0;
}
@Override
public List<User> listByPage(int index, int pageSize) {
Connection connection=JDBCUtil.getConnection();
PreparedStatement pstm=null;
ResultSet rs=null;
List<User> uList=new ArrayList<User>();
try {
String sqlString="select * from user limit ?,?";
pstm=connection.prepareStatement(sqlString);
pstm.setInt(1, (index-1)*pageSize);
pstm.setInt(2, pageSize);
rs=pstm.executeQuery();
while (rs.next()) {
User user=new User(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getInt(5));
uList.add(user);
}
return uList;
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtil.closeAll(rs, pstm, connection);
}
return null;
}
@Override
public User findUserByNamePwd(String username, String pwd) {
Connection connection=JDBCUtil.getConnection();
PreparedStatement pstm=null;
ResultSet rs=null;
User user=null;
try {
String sqlString="select * from user where user=?&&pwd=?";
pstm=connection.prepareStatement(sqlString);
pstm.setString(1, username);
pstm.setString(2, pwd);
rs=pstm.executeQuery();
while (rs.next()) {
user=new User(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getInt(5));
}
return user;
} catch (SQLException e) {
// TODO: handle exception
}finally {
JDBCUtil.closeAll(rs, pstm, connection);
}
return null;
}
@Override
public User findUserById(int id) {
Connection connection=JDBCUtil.getConnection();
PreparedStatement pstm=null;
ResultSet rs=null;
User user=null;
try {
String sqlstring="select * from user where id=?";
pstm=connection.prepareStatement(sqlstring);
pstm.setInt(1, id);
rs=pstm.executeQuery();
while (rs.next()) {
user=new User(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getInt(5));
}
return user;
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtil.closeAll(rs, pstm, connection);
}
return null;
}
}
项目结构:
7.创建单元测试类
点击UserDaoImp类右建->新建->其他
测试类代码:
package test;
import java.util.List;
import org.junit.Test;
import com.ly.daoimp.UserDaoImp;
import com.ly.entity.User;
public class UserDaoImpTest {
private UserDaoImp uDaoImp=new UserDaoImp();
@Test
public void testInsertUser() {
User user=new User();
user.setUsername("测试");
user.setPwd("123456");
user.setRole(1);
user.setStatus(0);
int num=uDaoImp.insertUser(user);
System.out.println(num);
if(num>0){
System.out.println("插入成功");
}
}
@Test
public void testDeleteUser() {
int num=uDaoImp.deleteUser(10);
if (num>0) {
System.out.println("删除数据成功");
}
}
@Test
public void testUpdateUser() {
User u=new User(4,"吴家伟","741852963",0,0);
int num=uDaoImp.updateUser(u);
if (num>0) {
System.out.println("更新数据成功");
}
}
@Test
public void testListByPage() {
List<User> users=uDaoImp.listByPage(1, 5);
for (User user : users) {
System.out.println(user);
}
}
@Test
public void testFindUserByNamePwd() {
User user=uDaoImp.findUserByNamePwd("ly", "123");
if (user!=null) {
System.out.println(user);
}
}
@Test
public void testFindUserById() {
User user=uDaoImp.findUserById(9);
if (user!=null) {
System.out.println(user);
}
}
}
点击UserDaoImpTest类右建->运行方式->JUnit测试
说明连接数据库成功,可以通过JDBC驱动来操作Mysql的ti’a添加和删除