[jsp] 심플 쿼리 브라우저, Simple Query Browser

web 2014. 3. 20. 15:48
반응형


jsp를 이용한 간단한 쿼리 브라우저



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<%@page contentType="text/html;charset=utf-8"%>
<%@page import='java.net.*, java.io.*, java.util.*, java.sql.*'%>
 
<%
    String defaultClass = "oracle.jdbc.driver.OracleDriver";
    String defaultUrl = "jdbc:oracle:thin:@localhost:1521:orcl";
    String defaultId = "id";
    String defaultPassword = "password";
%>
 
<html>
<head>
<title>Simple Query-Browser</title>
</head>
<body>
    <style>
* {
    font-family: Gulim;
    font-size: 12px;
}
 
.table td {
    border-bottom: 1px solid black;
}
 
.title {
    background-color: black;
    color: white;
    font-weight: bold;
}
 
.right {
    text-align: right;
}
</style>
 
<%
    String query = request.getParameter( "query" );
    String clazz = request.getParameter( "clazz" );
    String url = request.getParameter( "url" );
    String id = request.getParameter( "id" );
    String password = request.getParameter( "password" );
     
    query = query == null ? "" : query;
    clazz = clazz == null ? defaultClass : clazz;
    url = url == null ? defaultUrl : url;
    id = id == null ? defaultId : id;
    password = password == null ? defaultPassword : password;
%>
 
    <form method="post">
        <table>
            <tr>
                <td class='right'>class :</td>
                <td><input type="text" name="clazz" value="<%=clazz %>" size='50' /></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td class='right'>url :</td>
                <td><input type="text" name="url" value="<%=url %>" size='50' /></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td class='right'>id :</td>
                <td><input type="text" name="id" value="<%=id %>" size='50' /></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td class='right'>password :</td>
                <td><input type="text" name="password" value="<%=password %>" size='50' /></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td class='right'>query :</td>
                <td colspan='2'><input type="text" name="query" value="<%=query %>" size="100" /></td>
                <td><input type="submit" value="execute" /></td>
            </tr>
        </table>
    </form>
<%
    boolean check = true;
    if( query.isEmpty() )
    {
        check = false;
    }
    else if( clazz.isEmpty() )
    {
        check = false;
        out.println( "<script>alert('class is not null.')</script>");
    }
    else if( url.isEmpty() )
    {
        check = false;
        out.println( "<script>alert('url is not null.')</script>");
    }
    else if( id.isEmpty() )
    {
        check = false;
        out.println( "<script>alert('id is not null.')</script>");
    }
    else if( password.isEmpty() )
    {
        check = false;
        out.println( "<script>alert('password is not null.')</script>");
    }
     
    if( check )
    {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
         
        try
        {
            Class.forName( clazz );
            conn = DriverManager.getConnection( url, id, password );
            stmt = conn.createStatement();
         
            rs = stmt.executeQuery( query );
            ResultSetMetaData metaData = rs.getMetaData();
            int columns = metaData.getColumnCount();
         
            String[] titles = new String[columns];
            for( int i = 0; i < columns; i++ )
                titles[i] = metaData.getColumnName( i + 1 );
         
            ArrayList<String[]> rowArr = new ArrayList<String[]>();
            while( rs.next() )
            {
                String[] arr = new String[columns];
                for( int i = 0; i < columns; i++ )
                    arr[i] = rs.getString( ( i + 1 ) );
                rowArr.add( arr );
            }
         
            out.println( "<table class='table' cellspacing='0' cellpadding='2'>");
            out.println( "<tr>");
            for( String arr : titles )
                out.print( "<td class='title'>" + arr + "</td>" );
            out.println( "</tr>" );
             
            for( String[] arr : rowArr )
            {
                out.println( "<tr>" );
                for( String obj : arr )
                    out.print( "<td>" + obj + "</td>" );
                out.println( "</tr>" );
            }
             
            out.println( "</table>");
        }
        catch( Exception e )
        {
            out.println( e.getMessage() );
        }
    }
%>
</body>
</html>


반응형