public boolean findLensName(String companyname){
jdbcConnection(); //连接数据库
ResultSet rs=null;
try{
String str="select lens.lensname from lens,company where company.company='"+companyname+"' and company.companyid=lens.companyid";

System.out.println(str);
if(st.execute(str)){
System.out.println(rs.getString("lensname"));
System.out.println("asdfasdf");
return true;
}
System.out.println("<<<<<>>>s");
}catch(Exception e){
e.printStackTrace();
e.getMessage();
}
return false;
}
这个方法中参数是表中的记录.
为什么if()中的内容不执行呢..
这个sql语句是对的啊..我在mysql中检验过了.
怎么放在方法中,就不好用了呢.

解决方案 »

  1.   

    st还是个NULL变量吧.这个变量是连接数据库后,用数据库连接变量来建的,我看你连完数据库后没有createStatement();
      

  2.   

    楼上的说得对,之前得加句st.createStatement();再写if语句
      

  3.   

    对要定义Statement 
    Connection con = ConToOra.getConnection();
    Statement stmt = con.createStatement();
      

  4.   

    再看看书吧Statement 、 ResultSet是怎么来操作的接楼上的
    Statement stmt = con.createStatement();
    这句之后是要
    ResultSet rs = stmt.execute(sql);然后在if(rs.next){
    System.out.println(rs.getString("lensname"));
    System.out.println("asdfasdf");
    }
      

  5.   

    try{
     Class.forName("db.class");//such as:oracle.jdbc.driver.OracleDriver String str="select lens.lensname from lens,company where company.company='"+companyname+"' and company.companyid=lens.companyid";
    Connection conn = DriverManager.getConnection(url,user,pasword);
    Statement st = conn.createStatement();
    ResultSet rs = st.execute(str);
    while(rs.next()){
    System.out.println("XXXX")
    }
    ..................
    }
      

  6.   

    要用PreparedStatement,用?代替变量
      

  7.   

    if(st.execute(str))不执行,st应该是个null,看抛出的异常是不是空指针异常就知道了