-
DriverManager:依据数据库的不同,管理JDBC驱动
-
Connection: 负责连接数据库并担任传送数据的任务
-
Statement: 由 Connection 产生、负责执行SQL语句
-
ResultSet:负责保存Statement执行后所产生的查询结果
最初写法
package com.xinzhi.jdbc;
import com.xinzhi.model.User;
import com.xinzhi.utils.JDBCUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.Scanner;
public class JdbcDemo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入用户名");
String uname = in.next();
System.out.println("请输入密码");
String pw = in.next();
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取数据库连接对象 Connection
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/firstly","root","root");
// connection = JDBCUtils.getConnection();
//定义sql
// String sql = "select id,username,password,name,phone,email from user";
String sql = "select id,username,password,name,phone,email from user where username="
+"'"+
uname
+"'"+
"and password="
+"'"+
pw
+"'"
;
//获取执行sql语句的对象Statement
statement = connection.createStatement();//statement有sql注入风险
//执行sql,接受返回结果
resultSet = statement.executeQuery(sql);
ArrayList<User> users = new ArrayList<>();
//处理结果
if (resultSet.next()){
System.out.println("登录成功"+resultSet.getString(4)+"回来");
} else {
System.out.println("用户名或密码错误");
}
// 查询 while (resultSet.next()){
// User user= new User( resultSet.getInt(1),
// resultSet.getString(2),
// resultSet.getString(3),
// resultSet.getString(4),
// resultSet.getString(5),
// resultSet.getString(6));
// users.add(user);
// }
// System.out.println(users);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
//释放资源
JDBCUtils.relase(connection,statement,resultSet);
}
}
}
改变
package com.xinzhi.jdbc;
import com.xinzhi.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class JdbcDemo_1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入用户名");
String uname = in.next();
System.out.println("请输入密码");
String pw = in.next();
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
connection = JDBCUtils.getConnection();
//定义sql
String sql = "select id,username,password,name,phone,email from user where username=? and password=?";
System.out.println(sql);
try {
//获得数据库连接对象Connection
//获取执行sql语句的对象 Statement
statement = connection.prepareStatement(sql);
statement.setString(1,uname);
statement.setString(2,pw);
//执行sql,接受返回结果
resultSet = statement.executeQuery();
//处理结果
if (resultSet.next()){
System.out.println("登录成功,欢迎" + resultSet.getString(4)+"回来");
}else {
System.out.println("用户名或密码错误");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
JDBCUtils.relase(connection,statement,resultSet);
}
}
}
package com.xinzhi.utils;
import java.sql.*;
public class JDBCUtils {
private static String driver = "com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/firstly";
private static String username = "root";
private static String password = "root";
private JDBCUtils(){
}
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection(){
try {
Connection connection = DriverManager.getConnection(url,username,password);
return connection;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void relase(Connection connection, Statement statement, ResultSet resultSet){
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (statement!=null){
try {
statement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
案例
package com.xinzhi.dao.impl;
import com.xinzhi.model.User;
public interface IUseDao {
/*
* 用户注册
* @param user 用户信息
* @return 注册结果
* */
String reginster(User user);
/*
* 用户登录
* @param username 用户名
* @return password 密码
* @return 登录结果
* */
String login(String username,String password);
/*
* 修改用户信息
* @param user 用户信息
* @return 修改结果
* */
String update(User user);
/*
* 删除用户信息
* @param user 用户名
* @return 删除结果
* */
String delete(String username);
}
package com.xinzhi.dao.impl.impl;
import com.xinzhi.dao.impl.IUseDao;
import com.xinzhi.model.User;
import com.xinzhi.utils.JDBCUtils;
import com.xinzhi.utils.MD5Utiles;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDaoImpl implements IUseDao {
//注册
@Override
public String reginster(User user) {
PreparedStatement preparedStatement = null;
int i = -1;
Connection connection = com.xinzhi.utils.JDBCUtils.getConnection();
//用户信息校验
String username = user.getUsername();
if (username == null){
return "用户名不能为空";
}
username = username.trim();
if (username.length()<6){
return "用户名不能少于6位";
}
if (exisName(username)){
return "用户名已经被注册";
}
String password = user.getPassword();
if (password==null){
return "密码不能为空";
}
password = password.trim();
if (password.length()<8){
return "密码不能少于8位";
}
password = MD5Utiles.encrypt(password);//密码加密
String sql = "insert into user(username,password,name,phone,email) values(?,?,?,?,?)";
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
preparedStatement.setString(3,user.getName());
preparedStatement.setString(4,user.getPhone());
preparedStatement.setString(5,user.getEmail());
i = preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
JDBCUtils.relase(connection,preparedStatement,null);
}
return i>0?"注册成功":"注册失败";
}
//登录
@Override
public String login(String username, String password) {
Connection connection = com.xinzhi.utils.JDBCUtils.getConnection();
ResultSet resultSet = null;
PreparedStatement preparedStatement = null;
password = MD5Utiles.encrypt(password);
String sql = "select id,username,password,name,phone,email from user where username=? and password=?";
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()){
return "登录成功";
}
return "登录失败";
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
com.xinzhi.utils.JDBCUtils.relase(connection,preparedStatement,resultSet);
}
}
//修改
@Override
public String update(User user) {
Connection connection = com.xinzhi.utils.JDBCUtils.getConnection();
String sql = "update user set username=?,password=?,name=?,phone=?,email=? where id=?";
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,user.getUsername());
preparedStatement.setString(2,user.getPassword());
preparedStatement.setString(3,user.getName());
preparedStatement.setString(4,user.getPhone());
preparedStatement.setString(5,user.getEmail());
preparedStatement.setInt(6,user.getId());
int i = preparedStatement.executeUpdate();
return i>0?"修改成功":"修改失败";
} catch (SQLException e) {
throw new RuntimeException(e);
}finally {
com.xinzhi.utils.JDBCUtils.relase(connection,preparedStatement,null);
}
}
//删除
@Override
public String delete(String username) {
Connection connection = com.xinzhi.utils.JDBCUtils.getConnection();
String sql = "delete from user where username=?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,username);
int i = preparedStatement.executeUpdate();
return i > 0 ? "删除成功":"删除失败";
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/*
* 查询用户名是否存在
* @param username 用户名
* @return 是否存在
* */
private boolean exisName(String username){
Connection connection = com.xinzhi.utils.JDBCUtils.getConnection();
String sql = "select id,username,password,name,phone,email from user where username=?";
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,username);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()){
return true;
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return false;
}
}
//实现类
package com.xinzhi.model;
public class User {
private int id;
private String username;
private String password;
private String name;
private String phone;
private String email;
public User(int id, String username, String password, String name, String phone, String email) {
this.id = id;
this.username = username;
this.password = password;
this.name = name;
this.phone = phone;
this.email = email;
}
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
", phone='" + phone + '\'' +
", email='" + email + '\'' +
'}';
}
}
package com.xinzhi.utils;
import java.sql.*;
public class JDBCUtils {
private static String driver = "com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/firstly";
private static String username = "root";
private static String password = "root";
private JDBCUtils(){
}
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection(){
try {
Connection connection = DriverManager.getConnection(url,username,password);
return connection;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static void relase(Connection connection, Statement statement, ResultSet resultSet){
if (resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (statement!=null){
try {
statement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
package com.xinzhi.utils;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Utiles {
public static String encrypt(String password){
MessageDigest md = null;
String s = null;
try {
md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());
s = new BigInteger(1,md.digest()).toString(16);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
return s;
}
}