You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.5 KiB
80 lines
2.5 KiB
package com.zzy.dao.mysql;
|
|
|
|
import com.zzy.dao.AccountzzyDao;
|
|
import com.zzy.domain.Accountzzy;
|
|
|
|
import java.sql.*;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import static com.zzy.dao.mysql.DBHelper.url;
|
|
|
|
public class AccountzzyDaoImp implements AccountzzyDao {
|
|
@Override
|
|
public List<Accountzzy> findAll() {
|
|
ArrayList<Accountzzy> accountzzy = new ArrayList<Accountzzy>();
|
|
try {
|
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
|
// String url="jdbc:mysql://localhost:3306/food?userSSL=false&serverTimezone=Asia/Shanghai";
|
|
String user = "root";
|
|
String password ="159357";
|
|
|
|
Connection conn= DriverManager.getConnection(url,user,password);
|
|
String sql ="select *from users";
|
|
Statement sts =conn.createStatement();
|
|
ResultSet rs=sts.executeQuery(sql);
|
|
|
|
while(rs.next()) {
|
|
Accountzzy account1 =new Accountzzy();
|
|
((Accountzzy) account1).setUserid(((ResultSet) rs).getString("userid"));
|
|
account1.setUsername(rs.getString("username"));
|
|
account1.setAddress(rs.getString("address"));
|
|
account1.setPhone(rs.getString("phone"));
|
|
account1.setPassword(rs.getString("password"));
|
|
account1.add(account1);
|
|
|
|
}
|
|
rs.close();
|
|
sts.close();
|
|
conn.close();
|
|
}catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return accountzzy;
|
|
}
|
|
|
|
|
|
@Override
|
|
public Accountzzy findById(String userid) {
|
|
Accountzzy account = null;
|
|
try (java.sql.Connection connection = DBHelper.getConnection(); // 假设DBHelper类有getConnection方法
|
|
PreparedStatement pstmt = connection.prepareStatement("select * from users where userid = ?")) {
|
|
|
|
pstmt.setString(1, userid); // 设置参数
|
|
try (ResultSet rs = pstmt.executeQuery()) {
|
|
if (rs.next()) {
|
|
account = new Accountzzy();
|
|
// 类似地从rs中设置account的属性
|
|
}
|
|
}
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return account;
|
|
}
|
|
@Override
|
|
public int create(Accountzzy accountzzy) {
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public int modify(Accountzzy accountzzy) {
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public int remove(Accountzzy accountzzy) {
|
|
return 0;
|
|
}
|
|
}
|