JSP第六次作业
package jdbc;
import java.sql.Date;
public class User {
private int id;
private String name;
private int age;
private String email;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
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 jdbc;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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 close(Statement st, Connection con, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
((Connection) st).close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
public static void close(PreparedStatement st, Connection con, Object rs) {
// TODO Auto-generated method stub
}
}
package jdbc;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
// 提供添加方法
public boolean insert(User user) {
Connection con = null;
Statement st = null;
try {
// 连接对象
con = JDBCUtils.getcon();
// 获取执行sql语句
st = (Statement) con.createStatement();
// 执行sql语句
// java.util.Date birthday=user.getBirthday();
java.sql.Date sqlbirthday = new java.sql.Date(
new java.util.Date().getTime());
int row = ((java.sql.Statement) st)
.executeUpdate("insert into user(id,name,money,email,birthday)"
+ "values("
+ user.getId()
+ ",'"
+ user.getName()
+ "','"
+ user.getAge()
+ "','"
+ user.getEmail()
+ "','" + sqlbirthday + "'" + ")");
if (row > 0) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(st, con, null);
}
return false;
}
public void findall(){
Connection con = null;
java.sql.Statement st = null;
ResultSet rs = null;
try {
// 连接对象
con = JDBCUtils.getcon();
// 获取执行sql语句
st = con.createStatement();
//执行sql语句
rs=st.executeQuery("select * from user");
//遍历rs
while(rs.next()){
System.out.println(rs.getInt("id")+" "+rs.getString("name")+" "
+rs.getInt("money")+" "+rs.getString("email")+" "+rs.getDate("birthday")+"");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCUtils.close(st, con, rs);
}
}
//提供查询所有语句的方法
public List<User> findAllUser() {
Connection con = null;
Statement st = null;
ResultSet rs = null;
try {
// 连接对象
con = JDBCUtils.getcon();
// 获取执行sql语句
st = (Statement) con.createStatement();
//执行sql语句
rs=((java.sql.Statement) st).executeQuery("select * from user");
//遍历rs
List<User> list=new ArrayList<User>();
while(rs.next()){
User user=new User();
/*System.out.println(rs.getInt("id")+" "+rs.getString("name")+" "
+rs.getInt("money")+" "+rs.getString("email")+" "+rs.getDate("birthday")+"");
*/
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setEmail(rs.getString("email"));
user.setBirthday(rs.getDate("birthday"));
list.add(user);
}
return list;
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCUtils.close(st, con, rs);
}
return null;
}
//根据id查询对象
public User findbyid(int id){
Connection con = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
// 连接对象
con = JDBCUtils.getcon();
// 获取执行sql语句
st=con.prepareStatement("select * from user where id=?");
//执行sql语句
st.setInt(1, id);
rs=st.executeQuery();
//遍历rs
if(rs.next()){
User user=new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setEmail(rs.getString("email"));
user.setBirthday(rs.getDate("birthday"));
return user;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCUtils.close(st, con, rs);
}
return null;
}
//提供修改数据方法
public boolean update(User user){
Connection con = null;
PreparedStatement st = null;
try {
// 连接对象
con = JDBCUtils.getcon();
// 获取执行sql语句
st = con.prepareStatement("update user set age=?,name=? where id=?");
// 执行sql语句
st.setInt(3, user.getId());
st.setInt(1, user.getAge());
st.setString(2, user.getName());
int row = st.executeUpdate();
if (row > 0) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(st, con, null);
}
return false;
}
//提供删除数据方法
public boolean delete(int id){
Connection con = null;
PreparedStatement st = null;
try {
// 连接对象
con = JDBCUtils.getcon();
//获取执行sql语句
st = con.prepareStatement("delete from user where id=?");
// 执行sql语句
st.setInt(1, id);
int row = st.executeUpdate();
if (row > 0) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(st, con, null);
}
return false;
}
}
package jdbc;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
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 close(java.sql.Statement st, Connection con, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (st != null) {
((Connection) st).close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
public static void close(PreparedStatement st, Connection con, Object rs) {
// TODO Auto-generated method stub
}
}
package jdbc;
import java.sql.Date;
public class Jdbcinsettest {
public static void main(String[] args) {
// TODO Auto-generated method stub
UserDao dao=new UserDao();
User user=new User();
user.setId(1);
user.setName("djp");
user.setAge(21);
user.setEmail("369236941@qq.com");
user.setBirthday(new Date(2020-04-21));
boolean flag=dao.insert(user);
System.out.print(flag);
}
}
package jdbc;
import java.util.List;
public class Jdbccinserttest {
public static void main(String[] args) {
// TODO Auto-generated method stub
UserDao dao=new UserDao();
dao.findall();
List <User> list=dao.findAllUser();
System.out.print(list.size());
}
}
package jdbc;
public class Jdbccid {
public static void main(String[] args) {
// TODO Auto-generated method stub
UserDao dao=new UserDao();
User u=dao.findbyid(2);
System.out.println(u.getName());
}
}
package jdbc;
public class Jdbcupdate {
public static void main(String[] args) {
// TODO Auto-generated method stub
UserDao dao=new UserDao();
User user=new User();
user.setId(2);
user.setAge(20);
user.setName("zq");
boolean flag=dao.update(user);
System.out.print(flag);
}
}
package jdbc;
public class Jdbcdele {
public static void main(String[] args) {
// TODO Auto-generated method stub
UserDao dao=new UserDao();
User u=new User();
boolean flag=dao.delete(3);
System.out.print(flag);
}