package com.demo.dao;
import com.demo.bean.User;
import com.demo.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ForMe
* com.demo.dao
* 2018/12/10
* 17:19
*/
public class UserDAo {
public User Login(String name, String password){
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
User user = null;
try {
connection = JdbcUtils.getConn();
String sql = "select * from reader where name=? and password=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,name);
preparedStatement.setString(2,password);
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
user = new User();
user.setName(resultSet.getString("name"));
user.setPassword(resultSet.getString("password"));
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.close(connection,preparedStatement,resultSet);
}
return user;
}
public void addUser(User user){
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JdbcUtils.getConn();
String sql = "insert into reader(name,password,id,sex,telephone,email) values (?,?,?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,user.getName());
preparedStatement.setString(2,user.getPassword());
preparedStatement.setString(3,user.getId());
preparedStatement.setString(4,user.getSex());
preparedStatement.setString(5,user.getTelephone());
preparedStatement.setString(6,user.getEmail());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
JdbcUtils.close(connection,preparedStatement);
}
}
public boolean isExist(String username){
Connection connection = null;
String sql = "select * from reader where name=?";
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JdbcUtils.getConn();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,username);
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
return false;
}
} catch (SQLException e) {
e.printStackTrace();
}
return true;
}
public List<String> showBook(){//剩余各种书的信息
List<String> list = new ArrayList<String>();
int i = 0;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connection = JdbcUtils.getConn();
String sql = "select * from book";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
String message = "书名:" + resultSet.getString("book_name")
+ "<--->剩余数量:" + resultSet.getInt("book_num")
+ "<--->作者为:" + resultSet.getString("book_writer")
+ "<--->书的价格为:" + resultSet.getInt("book_price")
+ "<--->出版时间为:" + resultSet.getString("publish_time")
+ "<--->出版社为:" + resultSet.getString("publish_house");
list.add(message);
// System.out.println(message);
//System.out.println();
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JdbcUtils.close(connection,preparedStatement,resultSet);
}
return list;
}
}
白话机器学习
- 粉丝: 1w+
- 资源: 7650
最新资源
- C++-基于mfc开发连连看游戏-2025
- 基于Python(tkinter)的图书信息管理系统源码+实验报告(高分项目)
- C++ - MFC开发的Hex编辑器-2025
- 深度学习 Unet 实战分割项目:盐体图像分割数据集(二值图像分割任务)
- (Vim)(cuda12.4)mamba-ssm-1.1.1-cp310-cp310-win-amd64.whl
- 汽车质量投诉数据集(20240129-0429).zip
- MFC程序调用Matlab,后台执行;将返回数据更新到界面-介绍-MFC程序调用Matlab,后台执行;将返回数据更新到界面 .zip
- BC-CSharpDotNet-UserGuide【搜狗文档翻译_双语对照_英译中】.docx
- MFC程序创建word文档,并向word写入数据(包括表格等)-2025
- Vim windows (cuda12.4)安装包,包括causal-conv1d,mamba-ssm
- C++程序实现Csv文件读写功能,并将文件制作成静态库 -2025
- 1960-2023年世界各国相关指标数据集(1400+指标).xlsx
- 基于Python和Django框架的轻量化Web安全漏洞扫描系统
- MFC程序读写excel文件内容-点击MFC程序界面中“确认”按钮,读取excel文件内容;并选取其中部分内容更新到界面上编辑框中;将数据写入excel文件 .zip
- 基于机器学习+深度学习+bert方法的虚假新闻检测项目源码+文档说明(高分项目)
- 信捷 XDM 三伺服轴钻孔机实际案程序 配步科 HMI 手轮示教圆弧插信捷 XDM-32T4-E 系列的轴控制程序钻孔机步科的触摸屏,手轮 (编码器多种钻孔方式,方形矩阵等 型号 ET100示教圆形
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈