By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,650 Members | 1,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,650 IT Pros & Developers. It's quick & easy.

JavaBean to create db connection

P: 2
This is javabean code to create database connection.
You can use this code in any java application.
Expand|Select|Wrap|Line Numbers
  1. import java.sql.*;
  2.  
  3. public class DBConnect {
  4.  
  5.     private Connection con;
  6.     private String connstring;
  7.     private PreparedStatement ps;
  8.     private ResultSet rs;
  9.     private String tablename;
  10.  
  11.     public String getTablename() {
  12.         return tablename;
  13.     }
  14.  
  15.     public void setTablename(String tablename) {
  16.         this.tablename = tablename;
  17.     }
  18.  
  19.     public Connection getCon() {
  20.         return con;
  21.     }
  22.  
  23.     public void setCon(Connection con) {
  24.         this.con = con;
  25.     }
  26.  
  27.     public String getConnstring() {
  28.         return connstring;
  29.     }
  30.  
  31.     public void setConnstring(String connstring) {
  32.         this.connstring = connstring;
  33.     }
  34.  
  35.     public ResultSet getRs() {
  36.         return rs;
  37.     }
  38.  
  39.     public void setRs(ResultSet rs) {
  40.         this.rs = rs;
  41.     }
  42.  
  43.     public PreparedStatement getPs() {
  44.         return ps;
  45.     }
  46.  
  47.     public void setPs(PreparedStatement ps) {
  48.         this.ps = ps;
  49.     }
  50.  
  51.     public void setDriver() {
  52.         connstring = "org.postgresql.Driver";
  53.         try {
  54.             Class.forName(connstring);
  55.         } catch (ClassNotFoundException ex) {
  56.             System.out.print("Driver cannot be registered");
  57.         }
  58.     }//end of setDriver()
  59.  
  60.     public void startConnection() throws SQLException {
  61.         con = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/myshop", "postgres", "postgres");
  62.     }//end of startConnection()
  63.  
  64.     public void setStatement() throws Exception {
  65.         ps = con.prepareStatement("select * from " + tablename, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
  66.     }//end of setStatement()
  67. public void getQueryResult() throws Exception {
  68.         rs = ps.executeQuery();
  69.         //extractRS();
  70.     }//end of getQueryResult()
  71.  
  72.     public int getRows() throws Exception {
  73.         int totalrows = 0;
  74.         while (rs.next()) {
  75.             totalrows++;
  76.         }
  77.         rs.beforeFirst();
  78.         return totalrows;
  79.     }//end of getRows
  80.  
  81.     public int getCols() throws Exception {
  82.         ResultSetMetaData rsmd = rs.getMetaData();
  83.         int cols = rsmd.getColumnCount();
  84.         return cols;
  85.     }//end of getCols()
  86.  
  87.     public String[][] extractRS() throws Exception {
  88.         String arr[][] = new String[getRows()][getCols()];
  89.         int rows = 0;
  90.         while (rs.next()) {
  91.  
  92.             for (int i = 0; i < getCols(); i++) {
  93.                 arr[rows][i] = rs.getString(i + 1);
  94.             }//end of for loop
  95.             rows++;
  96.         }//end of while loop 
  97.         // con.close();
  98.         return arr;
  99.     }//end of extractRS()
  100. }//end of DBConnect class
  101.  
Nov 17 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.