行程统计
package com.Bean;
package com.Bean; public class Userbean { private String name; private String studentid; private String studentleibie; private String college; private String phonenumber; private String healthcolor; private String trip; private String other; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getStudentid() { return studentid; } public void setStudentid(String studentid) { this.studentid = studentid; } public String getStudentleibie() { return studentleibie; } public void setStudentleibie(String studentleibie) { this.studentleibie = studentleibie; } public String getCollege() { return college; } public void setCollege(String college) { this.college = college; } public String getPhonenumber() { return phonenumber; } public void setPhonenumber(String phonenumber) { this.phonenumber = phonenumber; } public String getHealthcolor() { return healthcolor; } public void setHealthcolor(String healthcolor) { this.healthcolor = healthcolor; } public String getTrip() { return trip; } public void setTrip(String trip) { this.trip = trip; } public String getOther() { return other; } public void setOther(String other) { this.other = other; } }
package com.Dao;
package com.Dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.Util.Userutil; import com.Bean.Userbean; public class Userdao { public void add(Userbean claz) throws ClassNotFoundException, SQLException { //获得链接对象 Connection connection = Userutil.getConnection(); //准备sql语句 String sql = "insert into coursexc(name,studentid,studentleibie,college,phonenumber,healthcolor,trip,other) values(?,?,?,?,?,?,?,?)"; PreparedStatement preparedStatement = null; try { //创建语句传输对象 preparedStatement = connection.prepareStatement(sql); // preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, claz.getName()); preparedStatement.setString(2, claz.getStudentid()); preparedStatement.setString(3, claz.getStudentleibie()); preparedStatement.setString(4, claz.getCollege()); preparedStatement.setString(5, claz.getPhonenumber()); preparedStatement.setString(6, claz.getHealthcolor()); preparedStatement.setString(7, claz.getTrip()); preparedStatement.setString(8, claz.getOther()); preparedStatement.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //关闭资源 //DBUtil.close(preparedStatement); //DBUtil.close(connection); } } }
package com.Util;
package com.Util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Userutil { public static Connection getConnection() throws ClassNotFoundException, SQLException { Connection connection = null;//连接数据库 Statement stmt = null;//Statement 对象用于将 SQL 语句发送到数据库中。 ResultSet rs = null; //1. 导入驱动jar包 //2.注册驱动 Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/xingcheng", "root", "123456"); return connection; } public static void close(Connection connection ) { try { if (connection != null) { connection.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(PreparedStatement preparedStatement ) { try { if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void close(ResultSet resultSet ) { try { if (resultSet != null) { resultSet.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }