import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.StringTokenizer;
import list.ConnOracle;class FileOperate {
private static Statement connc;
private  static Connection connOrac;
    public static void main(String[] args) throws IOException {
    
     try {
            Class.forName("com.mysql.jdbc.Driver").newInstance(); 
            connOrac = DriverManager.getConnection("jdbc:mysql://localhost/ip?user=root&password=1314521&useUnicode=true&characterEncoding=gb2312");
            connc=connOrac.createStatement();
            System.out.println("ok1");
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
           
           
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    
        BufferedReader reader = new BufferedReader(new FileReader("D:\\hello.txt"));
        String line,ans = null;
        try {
        while((line=reader.readLine())!=null)
           if(line.contains("TTL")){
            
        
                ans = search(line);
             //   System.out.println(line);
                if(!ans.equals("")){
                 StringTokenizer st = new StringTokenizer(ans);
                 //按记录一条一条输出
                 while(st.hasMoreTokens()){
                String number = st.nextToken();
                  String name = st.nextToken();
                  String door = st.nextToken();
                String number2 = st.nextToken();
                     String name2 = st.nextToken();
                     String door2 = st.nextToken();
                 
                     
                  System.out.println(name);
                     
 String sql="SELECT country ,  LOCAL        FROM iptable     WHERE StartIpText = '"+name+"'      "  ;
                 
                 
                    
                     try {
                         ResultSet rs = connc.executeQuery(sql);
                   //  int i = 0;
                       //  int rowCount = rs.getRow();//首先将指针移动到最后一条记录,获得总记录数
                         String[] arr=new String[10000];
                 int i=0;             
                          String no;
                
                      while(rs.next() ){
                       
                     String  country = rs.getString("country");
                       
                     String  Local = rs.getString("Local");
                     System.out.println(country);
                     System.out.println(Local); 
                      }
                     
                      
                      
                     }catch(Exception e){
                      e.printStackTrace();
                     
                     }
                     
                     
                     
                     
                     
                  
                 }
                }else{
                 System.out.println("没有可以匹配的信息");
                }
                
                
                
                
   }
    
    else{
        //如果输入为空,提示用户
    //    System.out.println("你没有输入需要寻找的信息");
       }
} catch (IOException e) {
   e.printStackTrace();
  }   
      }
   
    
private static String search(String str) {
  String all = "";
  File file = new File("D:\\hello.txt");
  try {
   BufferedReader br = new BufferedReader(new FileReader(file));
   String s = br.readLine();
   while(s!=null){
    if (s == null) {
     System.err.println("数据读完了!");
    } else {
     //判断当前读入的记录行中是否有输入的关键字,如果有保存起来
     if(s.indexOf(str)!=-1){
      all = all +" "+ s;
     }
    }
    s = br.readLine();
   }
   br.close();// 一定要关闭资源
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return all;
 }
}

解决方案 »

  1.   


    Entity.java实体类
    public class Entity {
    private String country;
    private String Local; public String getCountry() {
    return country;
    } public void setCountry(String country) {
    this.country = country;
    } public String getLocal() {
    return Local;
    } public void setLocal(String local) {
    Local = local;
    }}
    FileOperate.java
    public class FileOperate { public Entity query() throws FileNotFoundException {
    BufferedReader reader = new BufferedReader(new FileReader(
    "D:\\hello.txt"));
    String line, ans = null;
    Connection connc = null;
    Statement stmt = null;
    ResultSet rs = null;
    Entity entity = null;
    try {
    while ((line = reader.readLine()) != null)
    if (line.contains("TTL")) {
    ans = search(line);
    if (!ans.equals("")) {
    StringTokenizer st = new StringTokenizer(ans);
    // 按记录一条一条输出
    while (st.hasMoreTokens()) {
    String number = st.nextToken();
    String name = st.nextToken();
    String door = st.nextToken();
    String number2 = st.nextToken();
    String name2 = st.nextToken();
    String door2 = st.nextToken(); String sql = "SELECT country , LOCAL FROM iptable WHERE StartIpText = '"
    + name + "' "; try {
    connc = getCon();
    stmt = connc.createStatement();
    rs = stmt.executeQuery(sql);
    // int i = 0;
    // int rowCount =
    // rs.getRow();//首先将指针移动到最后一条记录,获得总记录数
    String[] arr = new String[10000];
    int i = 0;
    String no; while (rs.next()) {
    entity = new Entity();
    entity.setCountry(rs.getString("country")); entity.setLocal(rs.getString("Local"));
    } } catch (Exception e) {
    e.printStackTrace(); } }
    } } } catch (IOException e) {
    e.printStackTrace();
    }
    return entity;
    } /**
     * 获取Connection对象
     * 
     * @return
     */
    public Connection getCon() {
    Connection connOrac = null;
    try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    connOrac = DriverManager
    .getConnection("jdbc:mysql://localhost/ip?user=root&password=1314521&useUnicode=true&characterEncoding=gb2312");
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    } catch (Exception e) {
    e.printStackTrace();
    }
    return connOrac;
    } private static String search(String str) {
    String all = "";
    File file = new File("D:\\hello.txt");
    try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String s = br.readLine();
    while (s != null) {
    if (s == null) {
    System.err.println("数据读完了!");
    } else {
    // 判断当前读入的记录行中是否有输入的关键字,如果有保存起来
    if (s.indexOf(str) != -1) {
    all = all + " " + s;
    }
    }
    s = br.readLine();
    }
    br.close();// 一定要关闭资源
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return all;
    }
    }包自己导入哈。
      

  2.   

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>
    <jsp:useBean id="hello" class="Demo.Entity" scope="session" />
    <jsp:setProperty name="hello" property="*" />
    your username is:<jsp:getProperty name="hello" property="country"/>
    <br><br>
    your password is:<jsp:getProperty name="hello" property="Local"/>
    <br><br>
    <%
    out.println(hello.toString()); 
    %>
    </html>
      

  3.   


    FileOperate fo=new FileOperate();
    Entity entity=fo.query();
      

  4.   

    your country is:null your local is:null Demo.Entity@1581e80
      

  5.   

    把你那些<jsp:bean>都去了。用我写的那个。就可以得到查询后的Entity对象
      

  6.   

    Default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an explicit constructor