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;
}
}
基于ajax+jsp+servlet+mysql利用IDEA实现了图书管理系统.zip
版权申诉
182 浏览量
2023-06-28
15:55:13
上传
评论
收藏 1.21MB ZIP 举报
![avatar](https://profile-avatar.csdnimg.cn/1ad39e95093044228d03f0d7ce161a82_zy_dreamer.jpg!1)
![avatar-vip](https://csdnimg.cn/release/downloadcmsfe/public/img/user-vip.1c89f3c5.png)
白话机器学习
- 粉丝: 9798
- 资源: 7681
最新资源
- 一段Scratch语言的计算斐波那契数列的步骤流程
- 一段VisualBasic语言的计算斐波那契数列的代码
- RUBY-DEFAULT-CONTAINS
- 基于Go语言写的一段计算斐波那契数列的代码!
- vue-material v1.0.0-beta-10.2
- TbReactions(BaseModel)
- Blumentals Rapid PHP 2025 v18.1.0.264 是一款功能强大的PHP编辑器和开发工具
- do not edit .js files directly - edit src/index.jst
- request-validator.cpython-37
- pygments.lexers.javascript ~~~~~~~~~~~~~~~~~~~~~~~~~~
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)