473,383 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

JavaBean to create db connection

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
0 2286

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

Similar topics

5
by: lbolognini | last post by:
Hi all, is there an alternative way of: - create a connection object - open the connection - close the connection every time one has to run a query.
0
by: Sham Yemul | last post by:
Hello, We developed an application that has many data entry forms and data controls in Vb.net and Sql Server2000 as backend. For this application we created connection object on load event of...
2
by: jeremystein | last post by:
When I drop a SqlConnection on the web form designer, it's created in InitializeComponent. I'd like to use this connection in my Page_Load method, but I don't see anywhere that it's opened. Do I...
8
by: Joanna Carter [TeamB] | last post by:
Hi Folks I am just trying to get my head around whether I can use a single SQLConnection for the life of the application or whether I should create it only when needed. I want to create...
0
by: Karthik | last post by:
Hi all, I am novice to the VB .net I don't know How to create a Connection with SQL server Using the Microsoft.Practise.EnterpriseLibrary? Where do i give the ConnectionString? I am...
2
by: johnny | last post by:
How do you create multiple connection in the treads. Lets say I will have at most 5 threads and I want to create at most 5 connections. If I create a connection in the "worker method", does it...
3
by: jith87 | last post by:
Can u tell me how to create connection pools in JDBC ???? Ths type of creating is said to have reduced the processing time spent during connecting and disconnecting to the databases. Pls help...
3
eboschi
by: eboschi | last post by:
Hi all, i'm new to .net programming and i have some problems with database connection. I have written two web application. Both of them use ADODB Connection to Sql Server 2000 database. The problem...
3
by: ashdaredevil | last post by:
I have created a Project which has a SQL sever 2000 database and when I setup that project and installed it on another system which haven't sql server 2000 , the connection error displayed Is it...
1
by: atul dubey | last post by:
hello sir i m unable to create databse connectivity for windows application in c# plz send me codes for insert,update and delete for windows application with sql server 2008.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.