数据库的WEB维护页面

 

数据库为SQL SERVER 2000

<%--  
    Document   : execsql
    Created on : 
2009 - 7 - 17 23 : 26 : 42
    Author     : Administrator
--%>

<% @page contentType = " text/html "  pageEncoding = " GB2312 " %>
<% @page import = " java.sql.* "   %>

<%!
// 得到一个数据库的连接
    Connection getConnection() throws SQLException {
        String url 
=   " jdbc:sqlserver://localhost:1433;DatabaseName=contacts " ;

        String user 
=   " sa " ; //  这里替换成你自已的数据库用户名
        String password  =   "" ; //  这里替换成你自已的数据库用户密码
         return  DriverManager.getConnection(url, user, password);
    }

    
// 判断一个字符串是否为空
     boolean  isEmptyString(String str) {
        
return  str  ==   null   ||  str.length()  ==   0   ||  str.trim().length()  ==   0 ;
    }
%>

< html >
    
< head >
        
< meta http - equiv = " Content-Type "  content = " text/html; charset=GB2312 " >
        
< title > WEB数据库维护系统 < / title>
         < style >
            TD,INPUT,SELECT {font
- size:12px}
        
< / style>
         < script language = " javascript " >
            
function  checkvalue()
            {
   
            }
        
< / script>
     < / head>
     < body >
        
< center >
            WEB数据库维护系统
            
< p >
                
<!-- 显示输入框,供用户输入需要执行的SQL语句 -->
            
< table width = " 80% "  border = " 0 " >
                
< tr >< td > 请输入您要执行的SQL语句: < / td>< / tr >
                
< tr >
                
< form name = " main "  action = " execsql.jsp "  method = " POST "  onsubmit = " return checkvalue(); " >
                    
< td >
                        
< textarea name = " sql "  rows = " 15 "  cols = " 80 " >< / textarea>
                     < / td>
                     < td valign = " bottom " >< input type = " submit "  value = " 执行 "   / >< / td >
                
< / form>
                 < / tr>
             < / table>

            
< hr >
            
<%-- 接收用户的输入,显示查询的结果 --%>
            
<%
            
// 获得用户输入的SQL语句
            String sql  =  request.getParameter( " sql " );
            
if  ( ! isEmptyString(sql)) {
                out.println(
" 您所执行的sql语句为: "   +  sql  +   " <br> " );
                Connection conn 
=   null ;
                Statement st 
=   null ;
                ResultSet rs 
=   null ;
                
try  {
                    conn 
=  getConnection();
                    st 
=  conn.createStatement();
                    
if  (st.execute(sql)) {
                        
// 执行结果为
                        rs  =  st.getResultSet();

                        
// 得到ResultSet的描述信息
                        ResultSetMetaData rsmd  =  rs.getMetaData();
                        
// 得到ResultSet的列数目
                         int  nColumnCount  =  rsmd.getColumnCount();
                        out.println(
" 您的查询结果为:<br> " );
                        out.println(
" <table width=\ " 90 % \ "  border=\ " 1 \ " > " );
                        out.println(
" <tr> " );
                        
for  ( int  i  =   1 ; i  <=  nColumnCount; i ++ ) {
                            out.println(
" <td><b> "   +  rsmd.getColumnName(i)  +   " </b></td> " );
                        }
                        out.println(
" </tr> " );
                        
while  (rs.next()) {
                            out.println(
" <tr> " );
                            
for  ( int  i  =   1 ; i  <=  nColumnCount; i ++ ) {
                                out.println(
" <td> "   +  rs.getString(i)  +   " </td> " );
                            }
                            out.println(
" </tr> " );
                        }
                    } 
else  {
                        
// 执行结果为正数
                        out.println( " 更新记录的数目为: "   +  st.getUpdateCount());
                    }
                } 
catch  (SQLException ex) {
                    out.println(ex);
                } 
finally  {
                    
// 关闭,释放资源
                     try  {
                        
if  (rs  !=   null ) {
                            rs.close();
                        }
                    } 
catch  (Exception ex) {
                    }

                    
try  {
                        
if  (st  !=   null ) {
                            st.close();
                        }
                    } 
catch  (Exception ex) {
                    }

                    
try  {
                        
if  (conn  !=   null ) {
                            conn.close();
                        }
                    } 
catch  (Exception ex) {
                    }
                }
            }
            
%>
            
< / p>
         < / center>
     < / body>
< / html>

转载于:https://www.cnblogs.com/qixin622/archive/2009/07/18/1526039.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值