<% @ Page ContentType = "TEXT / HTML; Charset = GB2312" Language = "Java" Import = "java.sql. *" ErrorPage = ""%> <%! / / Convenient to see it here, it is not written as a JavaBean Class CN {// Connecting Database, here is MS-SQL as an example string jdbcdriver = "com.microsoft.jdbc.sqlser.sqlserverdriver"; // jdbc drive string connectionString = "JDBC: Microsoft : SQL Server: // localhost: 1433; DatabaseName = TEST "; // Database connection string string user =" sa "; // database username String pass =" "; // database password connection conn = null; resultset = NULL;
Public CN () {Try {class.Forname (jdbcdriver);} catch (classnotfoundexception e) {system.err.println (e.tostring ());}}
public ResultSet executeQuery (String sql) {rs = null; try {conn = DriverManager.getConnection (connectionString, user, pass); Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); rs = stmt.executeQuery (sql ); Catch (SQLEXCEPTION E) {system.err.println (e.tostring ());} Return Rs;}
public void executeUpdate (String sql) {try {conn = DriverManager.getConnection (connectionString, user, pass); Statement stmt = conn.createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); stmt.executeUpdate (sql);} catch (SQLException e) {system.err.println (e.tostring ());}}}
Class Tree {CN CONN = New CN (); public void init (javax.servlet.jsp.jspwriter out, javax.servlet.http.httpservletRequest request) throws exception {Out.println ("
public void buildTree (javax.servlet.jsp.JspWriter out, int parentid, int level) throws Exception {level ; ResultSet rs = conn.executeQuery ( "select * from tree where parentid =" parentid "order by id"); while (rs.next ()) {OUT.PRINTLN ("
rs.getstring ("message") "id =" rs.get ("ID") " span>"); out.println (" div>");}}} rs.close (); rs = null;} private boolean has_child (int parentid) throws Exception {ResultSet rs = conn.executeQuery ( "select * from tree where parentid =" parentid "order by id"); return rs.next ();}
Public string getoption () throws exception {string Option = ""; ResultSet = conn.executeQuery ("SELECT * from tree order"); while (rs.next ()) Option = "