Source Code Cross Referenced for DBInterface.java in  » Database-Client » GUAM » guam » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » GUAM » guam 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package guam;
002:
003:        import java.sql.*;
004:        import java.util.Vector;
005:        import java.util.Hashtable;
006:
007:        /**
008:         * Interface with the MySQL database to create connections, close connections and administer user data
009:         * @author dkc
010:         * @version 1.0
011:         */
012:
013:        public class DBInterface {
014:            private String currURL;
015:            private Connection conn;
016:            private String DBName;
017:            private boolean connected = false;
018:            long lastAccessed;
019:            private String[] Privileges = new String[16];
020:            public static final int ALL = 0;
021:            public static final int ALTER = 1;
022:            public static final int CREATE = 2;
023:            public static final int DELETE = 3;
024:            public static final int DROP = 4;
025:            public static final int FILE = 5;
026:            public static final int GRANT = 6;
027:            public static final int INDEX = 7;
028:            public static final int INSERT = 8;
029:            public static final int PROCESS = 9;
030:            public static final int REFERENCES = 10;
031:            public static final int RELOAD = 11;
032:            public static final int SELECT = 12;
033:            public static final int SHUTDOWN = 13;
034:            public static final int UPDATE = 14;
035:            public static final int USAGE = 15;
036:            private Hashtable cachedDBStructure = null;
037:            private boolean DEBUG;
038:
039:            /** Construct the object, load the JDBC driver */
040:            public DBInterface() {
041:                try {
042:                    if (System.getProperty("guam.debug").equals("yes")) {
043:                        DEBUG = true;
044:                    } else {
045:                        DEBUG = false;
046:                    }
047:                } catch (Exception e) {
048:                    DEBUG = false;
049:                }
050:                // Init privilege names;
051:                Privileges[0] = "ALL";
052:                Privileges[1] = "ALTER";
053:                Privileges[2] = "CREATE";
054:                Privileges[3] = "DELETE";
055:                Privileges[4] = "DROP";
056:                Privileges[5] = "FILE";
057:                Privileges[6] = "GRANT";
058:                Privileges[7] = "INDEX";
059:                Privileges[8] = "INSERT";
060:                Privileges[9] = "PROCESS";
061:                Privileges[10] = "REFERENCES";
062:                Privileges[11] = "RELOAD";
063:                Privileges[12] = "SELECT";
064:                Privileges[13] = "SHUTDOWN";
065:                Privileges[14] = "UPDATE";
066:                Privileges[15] = "USAGE";
067:                // Init JDBC connections to MySQL.
068:                try {
069:                    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
070:                } catch (Exception E) {
071:                    System.err
072:                            .println("Unable to load JDBC driver.  Make sure it is in your CLASSPATH");
073:                    if (DEBUG) {
074:                        E.printStackTrace();
075:                    }
076:                }
077:            }
078:
079:            /** Connect to a MySQL server
080:             *  @param dbHost Hostname to connect to
081:             *  @param userName User to log in as
082:             *  @param userPass Password for given user or null if no password set
083:             *  @param dbPort Port to connect or null for default
084:             *  @return true if the connection succeeded or false if there was a problem connecting
085:             */
086:            public boolean connect(String dbHost, String userName,
087:                    String userPass, String dbPort) {
088:                this .DBName = dbHost;
089:                String tmpPass;
090:                String tmpPort;
091:                String dbName = "mysql"; // use mysql database by default
092:                if (userPass != null) {
093:                    tmpPass = "&password=" + userPass;
094:                } else {
095:                    tmpPass = "";
096:                }
097:                if (dbPort != null) {
098:                    tmpPort = ":" + dbPort;
099:                } else {
100:                    tmpPort = "";
101:                }
102:                currURL = "jdbc:mysql://" + dbHost + tmpPort + "/" + dbName
103:                        + "?user=" + userName + tmpPass;
104:                if (dbConnect()) {
105:                    this .connected = true;
106:                    return true;
107:                } else {
108:                    return false;
109:                }
110:            }
111:
112:            /** Disconnect from server.  If not connected, does nothing.
113:             */
114:            public void disconnect() {
115:                if (this .connected) {
116:                    try {
117:                        conn.close();
118:                    } catch (Exception e) {
119:                        if (DEBUG) {
120:                            e.printStackTrace();
121:                        }
122:                    }
123:                    this .connected = false;
124:                    currURL = null;
125:                    cachedDBStructure = null;
126:                }
127:            }
128:
129:            /** Check to see if there is a connection open.
130:             *  @return true if there is a connection open, false if not.
131:             */
132:            public boolean isConnected() {
133:                return this .connected;
134:            }
135:
136:            /** Create a MySQL user on the server
137:             *  @param username Username to create
138:             *  @param password Password for user
139:             *  @return true if the user was created successfully, false if there was an error.
140:             */
141:            public boolean createUser(String username, String password) {
142:                if (!checkConnection()) {
143:                    return false;
144:                }
145:                // Creates a user with no permissions to do anything and no allowable hosts.
146:                String sql;
147:                if (password == null) {
148:                    sql = "GRANT " + Privileges[USAGE] + " ON * TO " + username;
149:                } else {
150:                    sql = "GRANT " + Privileges[USAGE] + " ON * TO " + username
151:                            + " identified by '" + password + "'";
152:                }
153:                return doSqlUpd(sql);
154:            }
155:
156:            /** Delete a user from the database user list.
157:             *  @param username The user to delete
158:             *  @return true if deletion was successful, false if there was an error
159:             */
160:            public boolean deleteUser(String username) {
161:                if (!checkConnection()) {
162:                    return false;
163:                }
164:                String uname;
165:                String uhost;
166:                if (username.indexOf("@") > -1) {
167:                    uname = username.substring(0, username.indexOf("@"));
168:                    uhost = username.substring(username.indexOf("@") + 1);
169:                } else {
170:                    uname = username;
171:                    uhost = "";
172:                }
173:                // First part: revoke all privileges from user
174:                String sqlOne = "REVOKE " + Privileges[ALL] + " ON * FROM "
175:                        + username;
176:                // Second part: delete user from users table;
177:                String sqlTwo = "DELETE FROM user WHERE User='" + uname
178:                        + "' AND Host='" + uhost + "'";
179:                doSqlUpd(sqlOne);
180:                return doSqlUpd(sqlTwo);
181:            }
182:
183:            /** Change a user's password
184:             *  @param username the user whose password is to be changed
185:             *  @param password the new password for the user
186:             *  @return true if the change was successful, false if there was an error
187:             */
188:            public boolean changeUserPass(String username, String password) {
189:                String un;
190:                if (username.endsWith("@%")) {
191:                    un = username.substring(0, username.length() - 2);
192:                } else {
193:                    un = username;
194:                }
195:                String sql = "SET PASSWORD for " + un + "=PASSWORD('"
196:                        + password + "')";
197:                return doSqlUpd(sql);
198:            }
199:
200:            /** Retrieve the list of users from the current database
201:             *  @return a vector of all the users registered in the database
202:             */
203:            public Vector getUserList() {
204:                String sql = "SELECT user, host, password from user";
205:                if (!checkConnection()) {
206:                    return null;
207:                }
208:                try {
209:                    Statement stmt = conn.createStatement();
210:                    ResultSet rs = stmt.executeQuery(sql);
211:                    Vector ret = new Vector();
212:                    while (rs.next()) {
213:                        String val = rs.getString(1);
214:                        val += (rs.getString(2) == "%") ? "" : "@"
215:                                + rs.getString(2);
216:                        val += (rs.getString(3) == "" || rs.getString(3) == null) ? " *"
217:                                : "";
218:                        ret.add(val);
219:                    }
220:                    return ret;
221:                } catch (Exception E) {
222:                    if (DEBUG) {
223:                        E.printStackTrace();
224:                    }
225:                    return null;
226:                }
227:            }
228:
229:            /** Returns the structure of the current database
230:             *  @return a Hashtable prepared for insertion into a JTree.  The structure is as follows:
231:             *  root->databases->tables->columns, where databases and tables are hashtables and columns is a
232:             *  Vector.  The keys in the hashtables are the name of the element, and the Vector elements
233:             *  are Strings.
234:             */
235:            public Hashtable getDBStructure() {
236:                // Check the cache
237:                if (cachedDBStructure != null) {
238:                    return cachedDBStructure;
239:                }
240:                // Iterate through all the tables getting the column structure.  Chuck it all into
241:                // a hierarchical Hashtable
242:                Hashtable res = new Hashtable();
243:                try {
244:                    Statement st1 = conn.createStatement();
245:                    ResultSet rs0 = st1.executeQuery("SHOW DATABASES");
246:                    Hashtable databases = new Hashtable();
247:                    while (rs0.next()) {
248:                        Hashtable tables = new Hashtable();
249:                        String this DB = rs0.getString(1);
250:                        Statement st2 = conn.createStatement();
251:                        ResultSet rs1 = st2.executeQuery("SHOW TABLES FROM "
252:                                + this DB);
253:                        while (rs1.next()) {
254:                            Vector columns = new Vector();
255:                            String this Table = rs1.getString(1);
256:                            Statement st3 = conn.createStatement();
257:                            ResultSet rs2 = st3
258:                                    .executeQuery("SHOW COLUMNS FROM " + this DB
259:                                            + "." + this Table);
260:                            while (rs2.next()) {
261:                                columns.add(rs2.getString(1));
262:                            }
263:                            tables.put(this Table, columns);
264:                        }
265:                        databases.put(this DB, tables);
266:                    }
267:                    res.put(DBName, databases);
268:                    cachedDBStructure = res;
269:                    return res;
270:                } catch (Exception e) {
271:                    if (DEBUG) {
272:                        e.printStackTrace();
273:                    }
274:                    res.put("Error getting DB Structure!", null);
275:                    return res;
276:                }
277:            }
278:
279:            /** Grant a global privilege to a specific user
280:             *  @param username User to grant privilege to
281:             *  @param privilege Privilege to be granted
282:             *  @return true if successful, false if there was an error
283:             */
284:            public boolean grantGlobalPriv(String username, int privilege) {
285:                String un;
286:                if (username.endsWith("@%")) {
287:                    un = username.substring(0, username.length() - 2);
288:                } else {
289:                    un = username;
290:                }
291:                String sql = "GRANT " + Privileges[privilege] + " ON *.* TO "
292:                        + un;
293:                if (!checkConnection()) {
294:                    return false;
295:                }
296:                return doSqlUpd(sql);
297:            }
298:
299:            /** Grant a database privilege to a specific user
300:             *  @param username User to grant privilege to
301:             *  @param database Database on which to grant privilege
302:             *  @param privilege Privilege to be granted
303:             *  @return true if successful, false if there was an error
304:             */
305:            public boolean grantDBPriv(String username, String database,
306:                    int privilege) {
307:                String un;
308:                if (username.endsWith("@%")) {
309:                    un = username.substring(0, username.length() - 2);
310:                } else {
311:                    un = username;
312:                }
313:                String sql = "GRANT " + Privileges[privilege] + " ON "
314:                        + database + ".* TO " + un;
315:                if (!checkConnection()) {
316:                    return false;
317:                }
318:                return doSqlUpd(sql);
319:            }
320:
321:            /** Grant a table privilege to a specific user
322:             *  @param username User to grant privilege to
323:             *  @param database Database on which to grant privilege
324:             *  @param table Table on which to grant privilege
325:             *  @param privilege Privilege to be granted
326:             *  @return true if successful, false if there was an error
327:             */
328:            public boolean grantTablePriv(String username, String database,
329:                    String table, int privilege) {
330:                String un;
331:                if (username.endsWith("@%")) {
332:                    un = username.substring(0, username.length() - 2);
333:                } else {
334:                    un = username;
335:                }
336:                String sql = "GRANT " + Privileges[privilege] + " ON "
337:                        + database + "." + table + " TO " + un;
338:                if (!checkConnection()) {
339:                    return false;
340:                }
341:                return doSqlUpd(sql);
342:            }
343:
344:            /** Grant a column privilege to a specific user
345:             *  @param username User to grant privilege to
346:             *  @param database Database on which to grant privilege
347:             *  @param table Table on which to grant privilege
348:             *  @param column Column on which to grant privilege
349:             *  @param privilege Privilege to be granted
350:             *  @return true if successful, false if there was an error
351:             */
352:            public boolean grantColumnPriv(String username, String database,
353:                    String table, String column, int privilege) {
354:                String un;
355:                if (username.endsWith("@%")) {
356:                    un = username.substring(0, username.length() - 2);
357:                } else {
358:                    un = username;
359:                }
360:                String sql = "GRANT " + Privileges[privilege] + " (column) ON "
361:                        + database + "." + table + " TO " + un;
362:                if (!checkConnection()) {
363:                    return false;
364:                }
365:                return doSqlUpd(sql);
366:            }
367:
368:            /** Grant a privilege on a list of columns to a specific user
369:             *  @param username User to grant privilege to
370:             *  @param database Database on which to grant privilege
371:             *  @param table Table on which to grant privilege
372:             *  @param columns Array of columns on which to grant privilege
373:             *  @param privilege Privilege to be granted
374:             *  @return true if successful, false if there was an error
375:             */
376:            public boolean grantColumnPriv(String username, String database,
377:                    String table, String[] columns, int privilege) {
378:                String un;
379:                if (username.endsWith("@%")) {
380:                    un = username.substring(0, username.length() - 2);
381:                } else {
382:                    un = username;
383:                }
384:                String sql = "GRANT " + Privileges[privilege] + " (";
385:                if (!checkConnection()) {
386:                    return false;
387:                }
388:                for (int i = 0; i < columns.length; i++) {
389:                    sql += columns[i] + ((i + 1 == columns.length) ? "" : ",");
390:                }
391:                sql += ") ON * TO " + un;
392:                return doSqlUpd(sql);
393:            }
394:
395:            /** Revoke a global privilege from a specific user
396:             *  @param username User to revoke privilege from
397:             *  @param privilege Privilege to be revoked
398:             *  @return true if successful, false if there was an error
399:             */
400:            public boolean revokeGlobalPriv(String username, int privilege) {
401:                String un;
402:                if (username.endsWith("@%")) {
403:                    un = username.substring(0, username.length() - 2);
404:                } else {
405:                    un = username;
406:                }
407:                String sql = "REVOKE " + Privileges[privilege]
408:                        + " ON *.* FROM " + un;
409:                if (!checkConnection()) {
410:                    return false;
411:                }
412:                return doSqlUpd(sql);
413:            }
414:
415:            /** Revoke a database privilege from a specific user
416:             *  @param username User to revoke privilege from
417:             *  @param database Database from which to revoke privilege
418:             *  @param privilege Privilege to be revoked
419:             *  @return true if successful, false if there was an error
420:             */
421:            public boolean revokeDBPriv(String username, String database,
422:                    int privilege) {
423:                String un;
424:                if (username.endsWith("@%")) {
425:                    un = username.substring(0, username.length() - 2);
426:                } else {
427:                    un = username;
428:                }
429:                String sql = "REVOKE " + Privileges[privilege] + " ON "
430:                        + database + ".* FROM " + un;
431:                if (!checkConnection()) {
432:                    return false;
433:                }
434:                return doSqlUpd(sql);
435:            }
436:
437:            /** Revoke a table privilege from a specific user
438:             *  @param username User to revoke privilege from
439:             *  @param database Database from which to revoke privilege
440:             *  @param table Table from which to revoke privilege
441:             *  @param privilege Privilege to be revoked
442:             *  @return true if successful, false if there was an error
443:             */
444:            public boolean revokeTablePriv(String username, String database,
445:                    String table, int privilege) {
446:                String un;
447:                if (username.endsWith("@%")) {
448:                    un = username.substring(0, username.length() - 2);
449:                } else {
450:                    un = username;
451:                }
452:                String sql = "REVOKE " + Privileges[privilege] + " ON "
453:                        + database + "." + table + " FROM " + un;
454:                if (!checkConnection()) {
455:                    return false;
456:                }
457:                return doSqlUpd(sql);
458:            }
459:
460:            /** Revoke a column privilege from a specific user
461:             *  @param username User to revoke privilege from
462:             *  @param database Database from which to revoke privilege
463:             *  @param table Table from which to revoke privilege
464:             *  @param column Column from which to revoke privilege
465:             *  @param privilege Privilege to be revoked
466:             *  @return true if successful, false if there was an error
467:             */
468:            public boolean revokeColumnPriv(String username, String database,
469:                    String table, String column, int privilege) {
470:                String un;
471:                if (username.endsWith("@%")) {
472:                    un = username.substring(0, username.length() - 2);
473:                } else {
474:                    un = username;
475:                }
476:                String sql = "REVOKE " + Privileges[privilege]
477:                        + " (column) ON " + database + "." + table + " FROM "
478:                        + un;
479:                if (!checkConnection()) {
480:                    return false;
481:                }
482:                return doSqlUpd(sql);
483:            }
484:
485:            /** Revoke a privilege on a list of columns from a specific user
486:             *  @param username User to revoke privilege from
487:             *  @param database Database from which to revoke privilege
488:             *  @param table Table from which to revoke privilege
489:             *  @param columns Array of columns from which to revoke privilege
490:             *  @param privilege Privilege to be revoked
491:             *  @return true if successful, false if there was an error
492:             */
493:            public boolean revokeColumnPriv(String username, String database,
494:                    String table, String[] columns, int privilege) {
495:                String un;
496:                if (username.endsWith("@%")) {
497:                    un = username.substring(0, username.length() - 2);
498:                } else {
499:                    un = username;
500:                }
501:                String sql = "REVOKE " + Privileges[privilege] + " (";
502:                if (!checkConnection()) {
503:                    return false;
504:                }
505:                for (int i = 0; i < columns.length; i++) {
506:                    sql += columns[i] + ((i + 1 == columns.length) ? "" : ",");
507:                }
508:                sql += ") ON * FROM " + un;
509:                return doSqlUpd(sql);
510:            }
511:
512:            /** Returns the global privileges for a given user
513:             *  @param username the name of the user whose privileges are to be returned
514:             *  @return a Vector containing strings of all the privileges the user has
515:             */
516:            public Vector getGlobalPrivs(String username) {
517:                String uname;
518:                String uhost;
519:                if (username.indexOf("@") > -1) {
520:                    uname = username.substring(0, username.indexOf("@"));
521:                    uhost = username.substring(username.indexOf("@") + 1);
522:                } else {
523:                    uname = username;
524:                    uhost = "";
525:                }
526:                String sql = "SELECT * FROM User where User='" + uname
527:                        + "' AND Host='" + uhost + "'";
528:                Vector retval = new Vector();
529:                int numPrivs = 0;
530:                try {
531:                    Statement stmt = conn.createStatement();
532:                    ResultSet rs = stmt.executeQuery(sql);
533:                    if (rs.next()) {
534:                        if (rs.getString("Select_priv").equals("Y")) {
535:                            numPrivs++;
536:                            retval.add("SELECT");
537:                        }
538:                        if (rs.getString("Insert_priv").equals("Y")) {
539:                            numPrivs++;
540:                            retval.add("INSERT");
541:                        }
542:                        if (rs.getString("Update_priv").equals("Y")) {
543:                            numPrivs++;
544:                            retval.add("UPDATE");
545:                        }
546:                        if (rs.getString("Delete_priv").equals("Y")) {
547:                            numPrivs++;
548:                            retval.add("DELETE");
549:                        }
550:                        if (rs.getString("Create_priv").equals("Y")) {
551:                            numPrivs++;
552:                            retval.add("CREATE");
553:                        }
554:                        if (rs.getString("Drop_priv").equals("Y")) {
555:                            numPrivs++;
556:                            retval.add("DROP");
557:                        }
558:                        if (rs.getString("Reload_priv").equals("Y")) {
559:                            numPrivs++;
560:                            retval.add("RELOAD");
561:                        }
562:                        if (rs.getString("Shutdown_priv").equals("Y")) {
563:                            numPrivs++;
564:                            retval.add("SHUTDOWN");
565:                        }
566:                        if (rs.getString("Process_priv").equals("Y")) {
567:                            numPrivs++;
568:                            retval.add("PROCESS");
569:                        }
570:                        if (rs.getString("File_priv").equals("Y")) {
571:                            numPrivs++;
572:                            retval.add("FILE");
573:                        }
574:                        if (rs.getString("Grant_priv").equals("Y")) {
575:                            numPrivs++;
576:                            retval.add("GRANT");
577:                        }
578:                        if (rs.getString("References_priv").equals("Y")) {
579:                            numPrivs++;
580:                            retval.add("REFERENCES");
581:                        }
582:                        if (rs.getString("Index_priv").equals("Y")) {
583:                            numPrivs++;
584:                            retval.add("INDEX");
585:                        }
586:                        if (rs.getString("Alter_priv").equals("Y")) {
587:                            numPrivs++;
588:                            retval.add("ALTER");
589:                        }
590:                        if (numPrivs == 0) {
591:                            retval.add("USAGE");
592:                        } else if (numPrivs == 14) {
593:                            retval.add("ALL");
594:                        }
595:                    } else {
596:                        //retval.add("USAGE");
597:                    }
598:                    return retval;
599:                } catch (Exception E) {
600:                    if (DEBUG) {
601:                        E.printStackTrace();
602:                    }
603:                    return null;
604:                }
605:            }
606:
607:            /** Returns database privileges for a given user
608:             *  @param username the name of the user whose privileges are to be returned
609:             *  @param DBName The name of the database
610:             *  @return a Vector containing strings of all the privileges the user has
611:             */
612:            public Vector getDBPrivs(String username, String DBName) {
613:                String uname;
614:                String uhost;
615:                if (username.indexOf("@") > -1) {
616:                    uname = username.substring(0, username.indexOf("@"));
617:                    uhost = username.substring(username.indexOf("@") + 1);
618:                } else {
619:                    uname = username;
620:                    uhost = "";
621:                }
622:                String sql = "SELECT * FROM db where User='" + uname
623:                        + "' AND Host='" + uhost + "' AND Db='" + DBName + "'";
624:                Vector retval = new Vector();
625:                int numPrivs = 0;
626:                try {
627:                    Statement stmt = conn.createStatement();
628:                    ResultSet rs = stmt.executeQuery(sql);
629:                    if (rs.next()) {
630:                        if (rs.getString("Select_priv").equals("Y")) {
631:                            numPrivs++;
632:                            retval.add("SELECT");
633:                        }
634:                        if (rs.getString("Insert_priv").equals("Y")) {
635:                            numPrivs++;
636:                            retval.add("INSERT");
637:                        }
638:                        if (rs.getString("Update_priv").equals("Y")) {
639:                            numPrivs++;
640:                            retval.add("UPDATE");
641:                        }
642:                        if (rs.getString("Delete_priv").equals("Y")) {
643:                            numPrivs++;
644:                            retval.add("DELETE");
645:                        }
646:                        if (rs.getString("Create_priv").equals("Y")) {
647:                            numPrivs++;
648:                            retval.add("CREATE");
649:                        }
650:                        if (rs.getString("Drop_priv").equals("Y")) {
651:                            numPrivs++;
652:                            retval.add("DROP");
653:                        }
654:                        if (rs.getString("Grant_priv").equals("Y")) {
655:                            numPrivs++;
656:                            retval.add("GRANT");
657:                        }
658:                        if (rs.getString("References_priv").equals("Y")) {
659:                            numPrivs++;
660:                            retval.add("REFERENCES");
661:                        }
662:                        if (rs.getString("Index_priv").equals("Y")) {
663:                            numPrivs++;
664:                            retval.add("INDEX");
665:                        }
666:                        if (rs.getString("Alter_priv").equals("Y")) {
667:                            numPrivs++;
668:                            retval.add("ALTER");
669:                        }
670:                        if (numPrivs == 0) {
671:                            retval.add("USAGE");
672:                        } else if (numPrivs == 10) {
673:                            retval.add("ALL");
674:                        }
675:                    } else {
676:                        //retval.add("USAGE");
677:                    }
678:                    return retval;
679:                } catch (Exception E) {
680:                    if (DEBUG) {
681:                        E.printStackTrace();
682:                    }
683:                    return null;
684:                }
685:            }
686:
687:            /** Returns table privileges for a given user
688:             *  @param username the name of the user whose privileges are to be returned
689:             *  @param DBName The name of the database
690:             *  @param TableName The name of the table
691:             *  @return a Vector containing strings of all the privileges the user has
692:             */
693:            public Vector getTablePrivs(String username, String DBName,
694:                    String TableName) {
695:                String uname;
696:                String uhost;
697:                if (username.indexOf("@") > -1) {
698:                    uname = username.substring(0, username.indexOf("@"));
699:                    uhost = username.substring(username.indexOf("@") + 1);
700:                } else {
701:                    uname = username;
702:                    uhost = "";
703:                }
704:                String sql = "SELECT Table_Priv FROM tables_priv where User='"
705:                        + uname + "' AND Host='" + uhost + "' AND Db='"
706:                        + DBName + "' AND Table_name='" + TableName + "'";
707:                Vector retval = new Vector();
708:                try {
709:                    Statement stmt = conn.createStatement();
710:                    ResultSet rs = stmt.executeQuery(sql);
711:                    if (rs.next()) {
712:                        String s = rs.getString(1);
713:                        retval = stringSplit(s, ',');
714:                    }
715:                } catch (Exception E) {
716:                    if (DEBUG) {
717:                        E.printStackTrace();
718:                    }
719:                    return null;
720:                }
721:                return retval;
722:            }
723:
724:            /** Returns column privileges for a given user
725:             *  @param username the name of the user whose privileges are to be returned
726:             *  @param DBName The name of the database
727:             *  @param TableName The name of the table
728:             *  @param ColName the name of the column
729:             *  @return a Vector containing strings of all the privileges the user has
730:             */
731:            public Vector getColumnPrivs(String username, String DBName,
732:                    String TableName, String ColName) {
733:                String uname;
734:                String uhost;
735:                if (username.indexOf("@") > -1) {
736:                    uname = username.substring(0, username.indexOf("@"));
737:                    uhost = username.substring(username.indexOf("@") + 1);
738:                } else {
739:                    uname = username;
740:                    uhost = "";
741:                }
742:                String sql = "SELECT Column_Priv FROM columns_priv where User='"
743:                        + uname
744:                        + "' AND Host='"
745:                        + uhost
746:                        + "' AND Db='"
747:                        + DBName
748:                        + "' AND Table_name='"
749:                        + TableName
750:                        + "' AND Column_name='" + ColName + "'";
751:                Vector retval = new Vector();
752:                try {
753:                    Statement stmt = conn.createStatement();
754:                    ResultSet rs = stmt.executeQuery(sql);
755:                    if (rs.next()) {
756:                        String s = rs.getString(1);
757:                        retval = stringSplit(s, ',');
758:                    }
759:                } catch (Exception E) {
760:                    if (DEBUG) {
761:                        E.printStackTrace();
762:                    }
763:                    return null;
764:                }
765:                return retval;
766:            }
767:
768:            private boolean doSqlUpd(String sql) {
769:                try {
770:                    Statement stmt = conn.createStatement();
771:                    int tmp = stmt.executeUpdate(sql);
772:                    return true;
773:                } catch (Exception E) {
774:                    if (DEBUG) {
775:                        System.err.println(sql);
776:                        E.printStackTrace();
777:                    }
778:                    return false;
779:                }
780:            }
781:
782:            private boolean dbConnect() {
783:                try {
784:                    conn = DriverManager.getConnection(currURL);
785:                    lastAccessed = System.currentTimeMillis();
786:                    return true;
787:                } catch (Exception e) {
788:                    if (DEBUG) {
789:                        e.printStackTrace();
790:                    }
791:                    return false;
792:                }
793:            }
794:
795:            private boolean checkConnection() {
796:                if (System.currentTimeMillis() > lastAccessed + 3600000) {
797:                    if (!dbConnect()) {
798:                        return false;
799:                    }
800:                }
801:                return true;
802:            }
803:
804:            private Vector stringSplit(String inStr, char separator) {
805:                Vector tmp = new Vector();
806:                String temp = "";
807:                for (int i = 0; i < inStr.length(); i++) {
808:                    if (inStr.charAt(i) != separator) {
809:                        temp = temp + inStr.charAt(i);
810:                    } else {
811:                        if (temp.length() != 0) {
812:                            tmp.add(new String(temp));
813:                            temp = "";
814:                        }
815:                    }
816:                }
817:                if (temp.length() != 0) {
818:                    tmp.add(new String(temp));
819:                }
820:                if (tmp.size() == 0) {
821:                    tmp.add(inStr);
822:                }
823:                return tmp;
824:            }
825:
826:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.