mysql配置: 建立sql库
root:root
password:root
表名:student
id int
name varchar
age int
MyEclipse项目目录
database.properties配置
student.java
package mybean;
public class Student {
private String name;
private int age;
private int id;
public Student(){
super();
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
studentDao.java
package my.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import com.mysql.jdbc.Statement;
import mybean.Student;
import util.DBInit;
import util.JDBCUtil;
public class StudentDao {
Student student=null;
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
ArrayList list=new ArrayList();
//获取数据库的所有学生信息
public ArrayList getAllStudent(){
con=JDBCUtil.getConnection();
try {
String sql="select * from student";
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
student=new Student();
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setId(rs.getInt("id"));
list.add(student);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtil.release(con, ps, rs);
}
return list;
}
//添加学生
public int addstudent(Student student) throws Exception{
con=JDBCUtil.getConnection();
int result=0;
String sql="insert into student(name,age) values(?,?)";
try {
ps = con.prepareStatement(sql);
ps.setString(1, student.getName());
ps.setLong(2, student.getAge());
result=ps.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtil.release(con, ps, rs);
}
return result;
}
//删除
public int delete(int id) throws Exception{
con=JDBCUtil.getConnection();
int result=0;
String sql="delete from student where id=?";
try {
ps=con.prepareStatement(sql);
ps.setInt(1, id);
result=ps.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtil.release(con, ps, rs);
}
return result;
}
//返回一个学生的信息
public ResultSet select(Student student){
con=JDBCUtil.getConnection();
ResultSet rs=null;
String sql="select * from student where id=?";
try {
ps=con.prepareStatement(sql);
ps.setInt(1,student.getId());
//ps.setString(1, student.getName());
//ps.setInt(2, student.getAge());
rs=ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//JDBCUtil.release(con, ps, rs);
}
return rs;
}
//删除
public int updata(Student student)throws Exception{
con=JDBCUtil.getConnection();
int result=0;
String sql="update student set name=?,age=? where id=?";
try {
ps=con.prepareStatement(sql);
ps.setString(1, student.getName());
ps.setInt(2, student.getAge());
ps.setInt(3, student.getId());
ps.executeUpdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JDBCUtil.release(con, ps, rs);
}
return result;
}
//public static void main(String[] args) {
//StudentDao stu=new StudentDao();
//ArrayList list1=new ArrayList();
//list1=stu.getAllStudent();
//for (int i = 0; i < list1.size(); i++) {
//Student s= (Student)list1.get(i);
//System.out.println(s.getName()+" "+s.getAge());
//}
//}
}
DBInit.java
package util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class DBInit {
//获取database
public static Properties initDB(){
Properties pro=new Properties();
try {
Class c=DBInit.class;
ClassLoader cl=c.getClassLoader();
InputStream in=cl.getResourceAsStream("database.properties");
pro.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pro;
}
}
JDBCUtil.java
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class JDBCUtil {
//连接数据库
public static Connection getConnection(){
Properties pro=DBInit.initDB();
String driver=pro.getProperty("driver");
String username=pro.getProperty("userName");
String password=pro.getProperty("password");
String url=pro.getProperty("url");
Connection con=null;
try {
Class.forName(driver);
con=DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
//关闭连接
public static void release(Connection con,PreparedStatement ps,ResultSet rs){
try {
if(rs!=null){
rs.close();
}
} catch (Exception e) {
// TODO: handle exception
}
try {
if(ps!=null){
ps.close();
}
} catch (Exception e) {
// TODO: handle exception
}
try {
if(con!=null){
con.close();
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
登录页面login.jsp
name | |
age | |
主页 Main.jsp
StudentDao stuDao=new StudentDao();
List list=stuDao.getAllStudent();
int i=1;
%>
用户管理系统 | |||
id | name | age | |
%>
增加doadd.jsp
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
int age=Integer.parseInt(request.getParameter("age"));
Student student=new Student();
student.setName(name);
student.setAge(age);
StudentDao stuDao=new StudentDao();
int result=stuDao.addstudent(student);
%>
删除dodelete.jsp
int id=Integer.parseInt(request.getParameter("id"));
StudentDao stuDao=new StudentDao();
int result=stuDao.delete(id);
%>
修改update.jsp
request.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
int age=Integer.parseInt(request.getParameter("age"));
int id=Integer.parseInt(request.getParameter("id"));
Student student=new Student();
student.setName(request.getParameter("name"));
student.setAge(age);
student.setId(id);
StudentDao stuDao=new StudentDao();
int rs=stuDao.updata(student);
%>
doupdata.jsp
request.setCharacterEncoding("utf-8");
StudentDao stuDao=new StudentDao();
Student student=new Student();
int id=Integer.parseInt(request.getParameter("id"));
student.setId(id);
ResultSet re=stuDao.select(student);
if(re.next()){%>
">name | " name="name"> |
age | " > |
登录页面
主页面
去我博客里应该有源码文件、、找找吧