数据的接收、处理、发送

后台实质:

插入、删除、修改

接收页面数据-----加工数据(其他格式数据转换成java数据)-----封装到JavaBean类里(方便数据的传递)----SQL语句(将javaBean类中的数据赋给SQL)-------插入、删除、修改

 

查询:

1.条件查询:

接收页面数据-----加工数据(其他格式数据转换成java数据)-----封装到JavaBean类里(方便数据的传递)----SQL语句(将javaBean类中的数据赋给SQL作为查询条件)------遍历结果集把数据封装到集合的对象中-----加工数据集合(将java数据转换成其他格式数据)--------发送页面

2.无条件查询:

SQL语句------遍历结果集把数据封装到集合的对象中-----加工数据集合(将java数据转换成其他格式数据)--------发送页面

 

页面实质:

页面发送数据(或收集、加工数据)----get、post(地址栏、超链接、表单提交)

页面接收数据--------加工数据(转换成响应的格式)-----赋值给对应的变量显示

 

servlet处理:

接收数据:

一:接收html片段数据

     request.getParameter("name");

     request.getParameter("password");

 

二:接收JSON数据

 

三:接收XML数据

 

 

DAO层用JDBC处理:

public void save(Employee e) throws Exception {
  Connection conn = null;
  try{
   conn = DBUtil.getConnection();
   PreparedStatement prep = conn.prepareStatement( "insert into king(name,salary,age) values(?,?,?)");//指定列
   prep.setString(1, e.getName());                         insert into king values(k_seq.nextval,?,?,?);//oracle的sequence
   prep.setDouble(2, e.getSalary());
   prep.setInt(3, e.getAge());

 

   //PreparedStatement prep =conn.prepareStatement("delete from king where id=?");
   //prep.setInt(1, e.getId());

 

   //PreparedStatement prep = conn.prepareStatement("update king set name=?,salary=?,age=?

                                                                                                                                    where id=?");
   //prep.setString(1, e.getName());
   //prep.setDouble(2, e.getSalary());
   //prep.setInt(3, e.getAge());
   //prep.setInt(4, e.getId());

 

   conn.setAutoCommit(false);// 禁止自动提交(若不用事务管理的话这“三个”可去掉)
   prep.executeUpdate();

   conn.commit();// 提交事务
  }catch(Exception e1){
   e1.printStackTrace();

   conn.rollback();// 回滚事务
   throw e1;
  }finally{
   DBUtil.close(conn);
  }
 }

 

 

public List<Employee> findAll() throws Exception {
  List<Employee> employees = new ArrayList<Employee>();
  Connection conn = null;
  try{
   conn = DBUtil.getConnection();
   PreparedStatement prep = conn.prepareStatement("select * from king");

   //PreparedStatement prep = conn.prepareStatement("select * from king where id=?");
   //prep.setInt(1, e.getId());
   ResultSet rst = prep.executeQuery();
   while(rst.next()){
    Employee e = new Employee();
    e.setName(rst.getString("name"));
    e.setSalary(rst.getDouble("salary"));
    e.setAge(rst.getInt("age"));
    e.setId(rst.getInt("id"));
    employees.add(e);
   }
  }catch(Exception e1){
   e1.printStackTrace();
   throw e1;
  }finally{
   DBUtil.close(conn);
  }
  return employees;
 }

 

 

 /*jdbc工具类:获取数据库连接,关闭连接等方法*/

public class DBUtil {
 private static String driver = ConfigUtil.getValue("driver");
 private static String url = ConfigUtil.getValue("url");
 private static String user = ConfigUtil.getValue("user");
 private static String pwd = ConfigUtil.getValue("pwd");
 private static ThreadLocal<Connection> connectionHoders = new ThreadLocal<Connection>();
 public static synchronized Connection getConnection2() throws Exception{
  //先从线程局部变量(看成是一个容器)中取
  Connection conn = connectionHoders.get();
  if(conn == null){
   conn = getConnection();
   //以当前线程对象作为key,以conn作为value,放到了一个HashMap里面。
   connectionHoders.set(conn);
  }
  return conn;
 }


 public static synchronized void close2(){
  //以当前线程对象作为key,从HashMap中取对应的value
  Connection conn = connectionHoders.get();
  if(conn != null){
   try {
    conn.close();
    connectionHoders.set(null);
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
 }
 
 
   //获得一个连接方法:
  public static Connection getConnection() throws Exception{
   Class.forName(driver);
   Connection conn = DriverManager.getConnection(url,user,pwd);
   return conn;
  }
 
  //关闭连接方法:
  public static void close(Connection conn){
   if(conn != null){
    try {
     conn.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
  }
  
  public static void close(ResultSet rst){
   if(rst != null){
    try {
     rst.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
  }
  
  public static void close(Statement stat){
   if(stat != null){
    try {
     stat.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
  }
  
  public static void close(ResultSet rst,Statement stat,Connection conn){
   close(rst);
   close(stat);
   close(conn);
  }
  
  public static void close(Statement stat,Connection conn){
   close(stat);
   close(conn);
  }
}

 

 

   /*读db.properties数据库连接配置文件的工具类*/

 

public class ConfigUtil {
 private static Properties props = new Properties();
 static{
  /*
   * ConfigUtil.class : 获得ConfigUtil的class对象。

   * ConfigUtil.class.getClassLoader:获得加载ConfigUtil的类加载器。
   * 类加载器的getResourceAsStream方法:会依据指定的路径查找文件,并且返回一个InputStream流。
   */
  InputStream ips  = ConfigUtil.class.getClassLoader() .getResourceAsStream("util/db.properties");
  //创建输入流读取连接配置文件
  try {
   props.load(ips);  
  } catch (IOException e) {
   e.printStackTrace();
   System.out.println("读取db.properties文件失败");
  }
 }

 

  /*db.properties数据库连接配置文件*/

#mysql
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/数据库名
user=root
pwd=

#oracle
#driver=oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:@192.168.0.26:1521:数据库名
#user=xxxxxx

#pwd=123456

 

 

输出数据:

一:输出html数据

 

二:输出JSON数据

 

三:输出XML数据

 

 

 

Action处理

Action中不用写方法接收数据了,直接写对应页面的数据属性变量就可以接收数据了(类型还是字符串型)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值