今日学习内容:
第一天 | 第二天 | 第三天 | 第四天 | 第五天 | |
所花时间(小时) | 5 | ||||
代码量(行) | 500 | ||||
博客量(篇) | 1 | ||||
了解到的知识点 | web系统 |
|
重新写一个简单的学生管理系统,连接mysql数据库,利用html+css+sqlserver+eclipse:
首先编写java类:
Dao.java
package dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import DBUtil.DBUtil; import entity.Student; public class Dao { public Dao() { // TODO Auto-generated constructor stub } public static List<Student> getAllStudents(){ //获取学生列表 List<Student> list=new ArrayList<Student>(); Connection con=DBUtil.getConnection(); ResultSet rs=null; String sql="select * from student"; try{ Statement statement=con.createStatement(); rs=statement.executeQuery(sql); while(rs.next()) { Student student=new Student(); student.setId(rs.getInt("id")); student.setAddress(rs.getString("address")); student.setBirthday(rs.getString("birthday")); student.setName(rs.getString("name")); student.setSex(rs.getString("sex")); list.add(student); } DBUtil.closeDB(con, statement, rs); }catch(Exception e){ e.printStackTrace(); } return list; } public static void addStudent(Student student) { //添加学生 String sql="insert into student(name,sex,birthday,address) " + "values('"+student.getName()+"','"+student.getSex()+"','"+student.getBirthday()+"','"+student.getAddress()+"')"; Connection connection=DBUtil.getConnection(); try { Statement statement=connection.createStatement(); statement.executeUpdate(sql); DBUtil.closeDB(connection, statement, null); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void deleteStudent(int id) { //删除学生 String sql="delete from student where id="+id; Connection connection=DBUtil.getConnection(); try { Statement statement=connection.createStatement(); statement.executeUpdate(sql); DBUtil.closeDB(connection, statement, null); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void updateStudent(Student student) { //修改学生 String sql="update student set name='"+student.getName()+"',sex='"+student.getSex()+"'," + "birthday='"+student.getBirthday()+"',address='"+student.getAddress()+"' " + "where id="+student.getId(); Connection connection=DBUtil.getConnection(); try { Statement statement=connection.createStatement(); statement.executeUpdate(sql); DBUtil.closeDB(connection, statement, null); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Student getStudent(int id) { Student student=new Student(); Connection connection=DBUtil.getConnection(); ResultSet rs=null; String sql="select * from student where id="+id; try { Statement statement=connection.createStatement(); rs=statement.executeQuery(sql); if(rs.next()) { student.setId(rs.getInt("id")); student.setAddress(rs.getString("address")); student.setBirthday(rs.getString("birthday")); student.setName(rs.getString("name")); student.setSex(rs.getString("sex")); } DBUtil.closeDB(connection, statement, rs); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return student; } }
DBUtil.java
package DBUtil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { private static String url = "jdbc:sqlserver://127.0.0.1:54595;DatabaseName=stu"; private static String username="rong"; private static String password="123456"; public static Connection getConnection() { Connection con=null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); con = DriverManager.getConnection(url,username,password); System.out.println("连接成功"); } catch (Exception e) { e.printStackTrace(); System.out.println("连接失败"); } return con; } public static void closeDB(Connection connection,Statement statement,ResultSet resultSet) { if(connection!=null) try { connection.close(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if(statement!=null) try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(resultSet!=null) try { resultSet.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public DBUtil() { // TODO Auto-generated constructor stub } public static void main(String[] args) throws SQLException { Connection connection=getConnection(); closeDB(connection, null, null); } }
Student.java
package entity; public class Student { private int id; private String name; private String sex; private String birthday; private String address; public Student() { // TODO Auto-generated constructor stub } 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }