2.16

今天主要还是javaweb的学习

主要是编写了一个可以满足判断输入在进行搜索的功能
主要就是利用正则表达式

package com.fin.dao;
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 com.fin.bean.user;
import com.fin.bean.food;
import com.fin.util.BaseConnection;
public class sdDao {
    public static List<food> find(String findthing)
    {
        
        List<food> list = new ArrayList<food>();
        
        String sql = "select * from food where 1=1 ";
        String shuzi= "^[0-9]+(.[0-9]{1,3})?$";
        boolean flag=findthing.matches(shuzi);
        if (flag==true) {
            sql += "and fprice like '%" + findthing + "%'";
        }
        else {
            sql += "and fname like '%" + findthing + "%'";
        }
    
        Connection conn = BaseConnection.getConnection();
        Statement state = null;
        ResultSet rs = null;
        
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            food cour = null;
            while (rs.next()) {
                
                int id = rs.getInt("id");
                String fcalories2 = rs.getString("fcalories");
                String fname2 = rs.getString("fname");
                String fprice2 = rs.getString("fprice");
                String fcanteen2 = rs.getString("fcanteen");
                cour = new food(id, fcalories2,fname2,fprice2,fcanteen2);
                list.add(cour);
            }
        } catch (SQLException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }finally
        {
            BaseConnection.close(rs, state, conn);
        }
        return list;
    }
   
    
    
    
    

}
原文地址:https://www.cnblogs.com/ljpljm/p/12319228.html