package com.zx.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCUtil {
private static String url="jdbc:mysql://localhost:3307/数据库名";
private static String username="root";
private static String password="******";
static {
try {
Class.forName("com.mysql.jdbc.Driver");
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
Connection connection = DriverManager.getConnection(url, username, password);
return connection;
}
}
package com.zx.dao.impl;
import com.zx.beans.User;
import com.zx.dao.UserDao;
import com.zx.util.JDBCUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserDaoImpl implements UserDao {
@Override
public void addUser(User user) {
Connection connection=null;
try {
connection = JDBCUtil.getConnection();
String sql = "insert into t_user1 values (null,?,?,?,?,?)";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,user.getUsername());
ps.setString(2,user.getPassword());
ps.setInt(3,user.getAge());
ps.setString(4,user.getPhone());
ps.setString(5,user.getAddress());
ps.execute();
connection.close();
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void deleteUser(int id) {
Connection connection=null;
try {
connection = JDBCUtil.getConnection();
String sql="Delete from t_user1 where id=?";
PreparedStatement ps = connection.prepareStatement(sql);
//如果sql语句中有?表示占位符,需要替换问号
ps.setInt(1,id);
ps.execute();
//关闭连接
connection.close();
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void updateUser(User user) {
Connection connection=null;
try {
connection = JDBCUtil.getConnection();
String sql="Update t_user1 set username=?,password=? where id=?";
PreparedStatement ps = connection.prepareStatement(sql);
//如果sql语句中有?表示占位符,需要替换问号
// 设置新的数据库信息
ps.setString(1,user.getUsername());
ps.setString(2,user.getPassword());
ps.setInt(3,user.getId());
ps.execute();
//关闭连接
connection.close();
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public List<User> qetUser() {
ArrayList<User> users = new ArrayList<User>();
Connection connection = null;
try {
connection = JDBCUtil.getConnection();
String sql="select * from t_user1 ";
PreparedStatement ps = connection.prepareStatement(sql);
//如果sql语句中有?表示占位符,需要替换问号
//将数据添加到数据库中,执行sql
ResultSet set= ps.executeQuery();
while (set.next()) {//判断是否有下一个结果
// System.out.println(set);//查到的是对象
int id = set.getInt("id");
String name = set.getString("username");
String pwd = set.getString("password");
int age = set.getInt("age");
String phone = set.getString("phone");
String address = set.getString("address");
User user = new User(id, name, pwd, age, phone,address);
users.add(user);
}
connection.close();
return users;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
@Override
public User getUserById(int id) {
User user=new User();
Connection connection = null;
try {
connection = JDBCUtil.getConnection();
String sql="select * from t_user1 where id=? ";
PreparedStatement ps = connection.prepareStatement(sql);
//如果sql语句中有?表示占位符,需要替换问号
ps.setInt(1,id);
//将数据添加到数据库中,执行sql
ResultSet set= ps.executeQuery();
while (set.next()) {//判断是否有下一个结果
// System.out.println(set);//查到的是对象
int i = set.getInt("id");
String name = set.getString("username");
String pwd = set.getString("password");
int age = set.getInt("age");
String phone = set.getString("phone");
String address = set.getString("address");
user = new User(i, name, pwd, age, phone, address);
}
connection.close();
//返回对象
return user;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
@Override
public boolean Login(String username, String password) {
Connection connection = null;
try{
connection = JDBCUtil.getConnection();
String sql = "select * from t_user1 where username=? and password=?";
PreparedStatement ps = connection.prepareStatement(sql);
//如果sql语句中有?表示占位符,需要替换问号
ps.setString(1,username);
ps.setString(2,password);
ResultSet set=ps.executeQuery();
while ((set.next())){
return true;
}
//将数据添加到数据库中,执行sql
ps.execute();
//关闭连接
connection.close();
}catch (SQLException e){
e.printStackTrace();
}
return false;
}
}