欧酷网

您的位置:主页>网络>

一个简单的统计学分系统的小程序

一个学分系统的小程序

 

jsp页面我就不写了, 下面我只写业务类吧

 

LoginAction.java

 

package com.cn.attence.action;
 import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
 import com.cn.attence.dao.DBConnect;
import com.opensymphony.xwork2.ActionSupport;
 public class LoginAction extends ActionSupport{
 private String username;
 private String password;
 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 execute() throws Exception{
    DBConnect db = new DBConnect();
  Connection conn = db.getConnection();
  Statement stmt = conn.createStatement();
  List list = new ArrayList();
  String sql = "select * from userinfo";
  ResultSet rs = stmt.executeQuery(sql);
 while( rs.next()){;
  if(rs.getString("username").equals(this.username)  && rs.getString("password").equals(this.password))
   return SUCCESS; 
 }
  return ERROR;
 }
}
 

 

 

 

注册控制层RegisterAction.java

 

package com.cn.attence.action;
 import java.sql.Connection;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 import com.cn.attence.dao.DBConnect;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
 public class RegisterAction extends ActionSupport {
 private String username;
 private String password;
 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;
 }
 @Override
 public String execute() throws Exception {
  // TODO Auto-generated method stub
  DBConnect db = new DBConnect();
  Connection conn = db.getConnection();
  Statement stmt = conn.createStatement();
  List list = new ArrayList();
  String sql = "insert into userinfo(username,password)  values('"+username+"','"+password+"')";
  stmt.executeUpdate(sql); 
  conn.commit();
  return SUCCESS;
  }
}
 


修改UpdatestudentAction.java

 

package com.cn.attence.action;
 import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 import javax.servlet.http.HttpServletRequest;
 import org.apache.struts2.ServletActionContext;
 import com.cn.attence.dao.DBConnect;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
 public class UpdatestudentAction extends ActionSupport{
 private int id;
 private String name;
 private String classe;
 private int checke;
 private String num;
 private String sum1;
  public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
  public String getSum1() {
  return sum1;
 }
 public void setSum1(String sum1) {
  this.sum1 = sum1;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getClasse() {
  return classe;
 }
 public void setClasse(String classe) {
  this.classe = classe;
 }
 public int getChecke() {
  return checke;
 }
 public void setChecke(int checke) {
  this.checke = checke;
 }
 public String getNum() {
  return num;
 }
 public void setNum(String num) {
  this.num = num;
 }
 public String execute() throws SQLException, FileNotFoundException, IOException, ClassNotFoundException 
 { 
 DBConnect db = new DBConnect();
 Connection conn = db.getConnection();
  List list = new ArrayList();
 String sql = "update  studentinfo  set name=?,classe=?,checke=?,num=?,sum1=?  where id="+id;
 System.out.println("sql语句是"+sql); 
 PreparedStatement stmt = conn.prepareStatement(sql);
 stmt.setString(1, name);
 stmt.setString(2, classe);
 stmt.setInt(3, checke);
 stmt.setString(4, num);
 stmt.setString(5, sum1); 
 stmt.executeUpdate(); 
 conn.close();
 ActionContext ctx = ActionContext.getContext();
 Map session = ctx.getSession();
 session.put("list", list);
 return SUCCESS;
 }
}
 


 

 

 

 

 

 

ShowstudentAction.java

 

package com.cn.attence.action;
 import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 import com.cn.attence.dao.DBConnect;
import com.cn.entity.StudentCredit;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
 public class ShowstudentAction extends ActionSupport{
public String execute() throws Exception{
    DBConnect db = new DBConnect();
  Connection conn = db.getConnection();
  Statement stmt = conn.createStatement();
  String sql = "select * from studentinfo";
  ResultSet rs = stmt.executeQuery(sql);
  List list = new ArrayList();
  while(rs.next()){
   StudentCredit student =new StudentCredit();  
   student.setId(rs.getInt("id"));
   student.setName(rs.getString("name"));
   student.setNum(rs.getString("num"));
   student.setClasse(rs.getString("classe"));
   student.setChecke(rs.getInt("checke"));
   list.add(student);
  }
  conn.close();
  ActionContext ctx = ActionContext.getContext();
  Map session = ctx.getSession();
  session.put("list", list);
  return SUCCESS;
 }
}
 

 

删除操作:DeleteAction.java

package com.cn.attence.action;
 import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
 import javax.servlet.http.HttpServletRequest;
 import org.apache.struts2.ServletActionContext;
 import com.cn.attence.dao.DBConnect;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
 public class DeleteAction extends ActionSupport{
  public String execute() throws SQLException, FileNotFoundException, IOException, ClassNotFoundException 
 {
  HttpServletRequest request = ServletActionContext.getRequest();
  String  id=request.getParameter("id");
  System.out.println("id号是"+id);
  ActionContext ct = ActionContext.getContext(); 
  DBConnect db = new DBConnect();
  Connection conn = db.getConnection();
  String sql = "delete from studentinfo where id ="+id+"";
  System.out.println(sql);
  Statement stmt = conn.createStatement();
  stmt.executeUpdate(sql);
    conn.commit();
  return SUCCESS;
 }
}
 

DBConnect.java

package com.cn.attence.dao;
 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
 public class DBConnect {
 public static Connection getConnection() {
  String url = "jdbc:mysql://localhost:3306/studentAttence?useUnicode=true&characterEncoding=gbk";
  String user = "root";
  String psw = "root";
  Connection conn = null;
  try {
   Class.forName("com.mysql.jdbc.Driver");
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
  try {
   conn = DriverManager.getConnection(url, user, psw);
   conn.setAutoCommit(false);
   return conn;
  } catch (SQLException e) {
   e.printStackTrace();
  }
  return null;
 }
}
 

StudentDao.java

package com.cn.attence.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 java.util.Map;
 import com.cn.entity.StudentCredit;
import com.opensymphony.xwork2.ActionContext;
   public class StudentDao {
 //条件查询
 /** 
  *  通过id 取得某个学生信息 
  * @param id 
  * @return 
  */  
 public StudentCredit getSuById(int id) throws Exception{ 
  StudentCredit student=null;
  DBConnect db = new DBConnect();
  Connection conn = db.getConnection();
  Statement stmt = null;
  try {
   stmt = conn.createStatement();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  String sql = "select * from studentinfo where id="+id;
  System.out.println("条件查询sql语句"+sql);
  ResultSet rs = stmt.executeQuery(sql);  
  if(rs.next()){
    student =new StudentCredit();
      student.setId(rs.getInt("id"));
   student.setName(rs.getString("name"));
   student.setNum(rs.getString("num"));
   student.setClasse(rs.getString("classe"));
   student.setChecke(rs.getInt("checke")); 
   student.setSum1(rs.getString("sum1")); 
   System.out.println("对象值"+student.getSum1());
  }
    conn.close();
  return student;  
 }
}
 

 

 

 

 

 

 

  • 点赞

  • 收藏

  • 分享

  •    
    • 文章举报

zhupengqq1

 
发布了117 篇原创文章 ·    获赞 1 ·    访问量 1408  

私信

关注

相关文章推荐