用JDBC联接ODBC数据源,库为xiruo,用户名为sa,密码为空,查询 select count(*) from tree_folder where parentid=ID,如果查询结果为0则返回假,否则为真。

解决方案 »

  1.   

    这个应该是我那个树里判断是否有树枝的函数,改成java代码如下:
    public boolean has_child(int id) {
    String cline="com.microsoft.jdbc.sqlserver.SQLServerDriver";//这里要有ms的JDBC,如果没有可以去microsoft的网站下载
    String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xiruo";
    String user="sa";
    String pass="";
    Connection conn=null;
    ResultSet rs=null;
    String sql="select * from tree_folder where parentid="+id;
    Class.forName(cline);
    conn=DriverManager.getConnection(url,user,pass);
    Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    rs=stmt.executeQuery(sql);
    if(rs.next())
      return true;
    else
      return false;
    }
      

  2.   

    to  beyond_xiruo()    还是你厉害^_^  是你的 代码谢谢你了 如果你能   把这个代码的 完整JSP代码给我的话
    我会感激不尽的!
      

  3.   

    是改成JSP了 我用的是一个JavaBean做的你的has_child函数可是有错
    你给我看看 可以嘛?
    package teachtest;import java.awt.*;
    import java.sql.*;public class ConServer {
      String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
      String sConnURL =
          "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=TeachTest";
      String username = "sa";
      String password = "9913089";
      Connection conn = null;
      ResultSet RS = null;
      public ConServer() {
        try {
          Class.forName(sDBDriver);
        }
        catch (java.lang.ClassNotFoundException e) {
          System.err.println("ConServer()" + e.getMessage());
        }
      }  public ResultSet executeQuery(String SQL) {
        RS = null;
        try {
          conn = DriverManager.getConnection(sConnURL, username, password);
          Statement stmt = conn.createStatement();
          RS = stmt.executeQuery(SQL);
        }
        catch (SQLException ex) {
          System.err.println("ConServer.executeQuery:" + ex.getMessage());
        }
        return RS;
      }  public boolean has_child(int id) {
        String strSQL = "select * from tree_folder where parentid=" + id;
        ResultSet RS =null;
        try {
          conn = DriverManager.getConnection(sConnURL, username, password);
          Statement stmt = conn.createStatement();
          RS = stmt.executeQuery(strSQL);
        }
        catch (SQLException ex) {
          System.err.println("ConServer.executeQuery:" + ex.getMessage());
        }
          if (RS.next())
            return true;
          else
            return false;
    }
    }
    "ConServer.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 58, column 14
      

  4.   

    把has_child函数改成如下这个样子试试
      public boolean has_child(int id) {
        String strSQL = "select * from tree_folder where parentid=" + id;
        ResultSet RS =null;
        try {
          RS=executeQuery(strSQL);
        }
        catch (SQLException ex) {
          System.err.println("ConServer.executeQuery:" + ex.getMessage());
          return false;
        }
          if (RS.next())
            return true;
          else
            return false;
    }
    }
      

  5.   

    catch (SQLException ex) 
    ^^^^^^
    有错!  我Java 也比较菜!"ConServer.java": Error #: 556 : exception java.sql.SQLException is never thrown in the corresponding try block at line 51, column 7
      

  6.   

    to    icecloud    去掉是一样的
    你看看 错误消息
      

  7.   

    public boolean has_child(int id) {
        String strSQL = "select * from tree_folder where parentid=" + id;
        ResultSet RS = null;
        try {
          RS = executeQuery(strSQL);
          }
          catch (SQLException ex) {
          System.err.println("ConServer.executeQuery:" + ex.getMessage());
            }while(RS.next)
        {
    rowscont++;
    }    if (rowscount!=0)
          return true;
        else
          return false;
      }
      

  8.   

    to   GaoLun(★米老鼠★) 
    还是有错!
    catch (SQLException ex) 
    ^^^^^^
    有错!  我Java 也比较菜!"ConServer.java": Error #: 556 : exception java.sql.SQLException is never thrown in the corresponding try block at line 51, column 7
      

  9.   

    直接改成
    catch(Exception ex)
      

  10.   

    beyond_xiruo()      
    还是有错!
      

  11.   

    RS.next
    ^^^^
    这里错"ConServer.java": Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 53, column 15
      

  12.   

    public boolean has_child(int id) {
        String strSQL = "select * from tree_folder where parentid=" + id;
        try {
          ResultSet RS=executeQuery(strSQL);
          if(RS.next())
            return true;
          else
            return false;
        } catch (Exception ex) {
          System.err.println("ConServer.executeQuery:" + ex.getMessage());
          return false;
        }
    }
    }
      

  13.   

    or
    public boolean has_child(int id) throws Exception {
        String strSQL = "select * from tree_folder where parentid=" + id;
        ResultSet RS=executeQuery(strSQL);
        if(RS.next())
          return true;
        else
          return false;
    }