package com.test.jdbc;
import java.util.Date;
public class User{
private int id;
private String username;
private String password;
private String email;
private Date birthDay;
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
}
package com.test.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtils {
public static Connection getCon() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "root");
return con;
}
public static void realse (ResultSet rs,Statement stmt, Connection con){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.test.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class UsersDao {
public boolean insert(User user1){
Connection con=null;
Statement stmt=null;
try{
con=JDBCUtils.getCon();
stmt = con.createStatement();
java.util.Date birthday=user1.getBirthDay();
String sqlBirthDay=String.format("%tF", birthday);
String sql="insert into user(id,name,password,email,birthday)"+"values('"
+user1.getId()+"','"
+user1.getUsername()+"','"
+user1.getPassword()+"','"
+user1.getEmail()+"','"
+sqlBirthDay+"'"
+")";
int row=stmt.executeUpdate(sql);
if(row>0){
return true;
}
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JDBCUtils.realse(null, stmt, con);
}
return false;
}
public List<User> findAllUser(){
Connection con =null;
Statement stmt =null;
ResultSet rs =null;
try{
con=JDBCUtils.getCon();
stmt=con.createStatement();
String sql="select * from user";
rs=stmt.executeQuery(sql);
List<User> list=new ArrayList<User>();
while(rs.next()){
User user1=new User();
user1.setId(rs.getInt("id"));
user1.setUsername(rs.getString("name"));
user1.setPassword(rs.getString("password"));
user1.setEmail(rs.getString("email"));
java.sql.Date birthday=rs.getDate("birthday");
user1.setBirthDay(birthday);
list.add(user1);
}
return list;
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JDBCUtils.realse(rs, stmt, con);
}
}
public User findUserById(int id){
Connection con =null;
PreparedStatement stmt=null;
ResultSet rs =null;
try{
con=JDBCUtils.getCon();
String sql="select * from user where id=?";
stmt=con.prepareStatement(sql);
stmt.setInt(1, id);
rs=stmt.executeQuery();
if(rs.next()){
User user1=new User();
user1.setId(rs.getInt("id"));
user1.setUsername(rs.getString("name"));
user1.setPassword(rs.getString("password"));
user1.setEmail(rs.getString("email"));
java.sql.Date birthday=rs.getDate("birthday");
user1.setBirthDay(birthday);
return user1;
}
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JDBCUtils.realse(rs, stmt, con);
}
return null;
}
public boolean update(User user1){
Connection con=null;
PreparedStatement stmt=null;
try{
con=JDBCUtils.getCon();
String sql="update user set name=?,password=? where id=?";
stmt = con.prepareStatement(sql);
stmt.setString(1, user1.getUsername());
stmt.setString(2, user1.getPassword());
stmt.setInt(3, user1.getId());
int row=stmt.executeUpdate();
if(row>0){
return true;
}
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JDBCUtils.realse(null, stmt, con);
}
return false;
}
public boolean delete (int id){
Connection con=null;
PreparedStatement stmt=null;
try{
con=JDBCUtils.getCon();
String sql="delete from user where id=?";
stmt = con.prepareStatement(sql);
stmt.setInt(1,id);
int row=stmt.executeUpdate();
if(row>0){
return true;
}
}catch(Exception e){
throw new RuntimeException(e);
}finally{
JDBCUtils.realse(null, stmt, con);
}
return false;
}
}
package com.test.jdbc;
import java.util.Date;
public class JdbcInsertTest {
public static void main(String[] args) {
UsersDao dao=new UsersDao();
User user1=new User();
user1.setId(2);
user1.setUsername("qs");
user1.setPassword("123345");
user1.setEmail("435752119@qq.com");
user1.setBirthDay(new Date());
boolean flag=dao.insert(user1);
}
}
package com.test.jdbc;
import java.util.List;
public class FindAllUserTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
UsersDao dao = new UsersDao();
List<User> list=dao.findAllUser();
System.out.println(list.size());
}
}
package com.test.jdbc;
public class FindUserByIdTest {
public static void main(String[] args) {
UsersDao dao = new UsersDao();
User u=dao.findUserById(3);
System.out.println(u.getUsername());
}
}
package com.test.jdbc;
public class UpdateUserTest {
public static void main(String[] args) {
UsersDao dao=new UsersDao();
User u=new User();
u.setId(3);
u.setUsername("mkx");
u.setPassword("123345");
boolean flag=dao.update(u);
System.out.println(flag);
}
}
package com.test.jdbc;
public class DeleteUserTest {
public static void main(String[] args) {
UsersDao dao=new UsersDao();
boolean flag=dao.delete(1);
System.out.println(flag);
}
}