Author Topic: Simpler Interface for Database Operations in Java  (Read 2463 times)

bbasujon

  • Administrator
  • VIP Member
  • *****
  • Posts: 1827
  • I want to show my performance at any where
    • View Profile
    • Higher Education
Simpler Interface for Database Operations in Java
« on: January 27, 2012, 08:35:48 AM »
Database operations in Java are sometimes cumbersome to the programmers. Also the existing methods in the Database classes are not that impressive or flexible. To reduce redundancy in the code and have a better programming design, I usually use a separate class for Database that fits all my needs and makes your other codes look clean. This class will basically have the following features.

    Constructor to give the DB details such as Database Drive Name, Database URl, Database Name, Username and Password.
    Execute Method to perform executeQuery().
    Updatea Method to perform executeUpdate().
    Method to close the Database connection.

Simple Database Connector Interface for Java

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

   

import java.sql.*;
public class Database_connector
{
   public Database_connector(String db_driver, String db_url,
         String db_dbName, String db_user, String db_pwd) {
      super();
      try
      {
         this.db_driver = db_driver;
         this.db_url = db_url;
         this.db_dbName = db_dbName;
         this.db_user = db_user;
         this.db_pwd = db_pwd;
      }
      catch(Exception e)
      {
         System.out.println("Error in constructor of Database Connector");
      }
   }
 
   String db_driver;
   String db_url;
   String db_dbName;
   String db_user;
   String db_pwd;
/* Start of Getters & Setters Method */
   public String getDb_driver() {
      return db_driver;
   }
 
   public void setDb_driver(String db_driver) {
      this.db_driver = db_driver;
   }
 
   public String getDb_url() {
      return db_url;
   }
 
   public void setDb_url(String db_url) {
      this.db_url = db_url;
   }
 
   public String getDb_dbName() {
      return db_dbName;
   }
 
   public void setDb_dbName(String db_dbName) {
      this.db_dbName = db_dbName;
   }
 
   public String getDb_user() {
      return db_user;
   }
 
   public void setDb_user(String db_user) {
      this.db_user = db_user;
   }
 
   public String getDb_pwd() {
      return db_pwd;
   }
 
   public void setDb_pwd(String db_pwd) {
      this.db_pwd = db_pwd;
   }
/* End of Getters & Setters Method */
 
   Connection con = null;
 
/* Start of Close Method */
   public void close()
   {
      try{
         con.close();
      }
      catch(Exception e)
      {
         System.out.println("Error in closing database");
      }
   }
/* End of Close Method */
 
/* Start of Execute Method */
   public ResultSet execute(String query) throws Exception{
      ResultSet rs=null;
      Statement stat = null;
      try
      {
         Class.forName(db_driver).newInstance();
         con=DriverManager.getConnection(db_url+db_dbName,db_user,db_pwd);
         stat=con.createStatement();
         rs=stat.executeQuery(query);
      }
      catch(Exception e)
      {
         System.out.println(e.getMessage());
      }
      return rs;
   }
/* End of Execute Method */
 
/* Start of Update Method */
   public int update(String query) throws Exception{
            Statement stat = null;
      try
      {
         Class.forName(db_driver).newInstance();
         con=DriverManager.getConnection(db_url+db_dbName,db_user,db_pwd);
         stat=con.createStatement();
         return(stat.executeUpdate(query));
      }
      catch(Exception e)
      {
         System.out.println(e.getMessage());
      }
      return -1;
   }
/* End of Update Method */
}
Acquire the knowledge and share the knowledge so that knowing,learning then sharing - all are the collection