Important alert: (current site time 7/15/2013 2:49:15 PM EDT)
 

VB icon

A SQL Connection Class

Email
Submitted on: 12/5/2002 8:33:47 AM
By: Xolani  
Level: Beginner
User Rating: By 3 Users
Compatibility: Java (JDK 1.2)
Views: 24953
author picture
(About the author)
 
     A simple connection class to SQL database via the odbc ....!!!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: A SQL Connection Class
// Description:A simple connection class to SQL database via the odbc ....!!!
// By: Xolani
//
// Inputs:Available methods:
SqlConn sqlCon = new SqlConn();
sqlCon.isConectionOk(url)
sqlCon.initConn(url);
Resultset rss = sqlCon.returnRecods(sql);
sqlCon.execQuery(sql);
sqlCon.CloseConn();
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=3315&lngWId=2//for details.//**************************************

/* SQL Connection by @ Xolani Gwaqa 2002 - 
***** Modify it to accept UserName and Password or to read them from the file ********
*/
import java.sql.*;
public class SqlConn{
	
 public Connection conn;
 public Statement stmt;
 public ResultSet rs;
 public SqlConn(){
	 conn = null;
	 stmt = null;
	 rs = null;
 }
	//connects to the dabase 
 public void initConn(String strUrl){
 if(strUrl.equals("")||(strUrl=="null")){
 strUrl = "jdbc:odbc:monthly";
 }
 
	 try{
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
	 }
	 catch(Exception e){
	 	 System.out.println(e.toString());
	 }
	 try{
	 conn = DriverManager.getConnection(strUrl,"sa","");
	 }
	 catch(Exception e){
	 System.out.println("Could not load Driver"); 
	}	
 }
//checks if the connection is established 
 public boolean isConectionOk(String strUrl){
 
 
 if(strUrl.equals("")||(strUrl=="null")){
 strUrl = "jdbc:odbc:monthly";
 }
 
	 try{
	 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
	 }
	 catch(Exception e){
	 	 System.out.println(e.toString());
	 	 return false;
	 }
	 try{
	 conn = DriverManager.getConnection(strUrl,"sa","");
	 }
	 catch(Exception e){
	 System.out.println("Could not load Driver"); 
	 return false;
	 }	
	 
	 return true;
	 
 }
 	//returns the connection back ... it helps if you want you have you own con inside you pages
 
 public Connection getConnection(String strUrl){ 
 if(strUrl.equals("")||(strUrl=="null")){
 strUrl = "jdbc:odbc:monthly";
 }
	 try{
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
	 }
	 catch(Exception e){
	 	 System.out.println(e.toString() + "sun.jdbc.odbc.JdbcOdbcDriver - failed");
	 }
	 try{
	 conn = DriverManager.getConnection(strUrl,"sa","");
	 }
	 catch(Exception e){
		 System.out.println("Could not load Driver"); 
	 }	
	 
	 return conn;
 }
	//returns the recordset 
 public ResultSet returnRecods(String strSQL)
 {
	 try{			
		 stmt = conn.createStatement();		
		 rs = stmt.executeQuery(strSQL);
	 }
	 catch(SQLException e){
		 System.out.println(e.toString() + " returnRecods() method failed"); 
	 }
	 return rs;
}
	//executes the query ..
 public void execQuery(String strSQL){
 	 try{
	stmt = conn.createStatement(); 
stmt.executeUpdate(strSQL);
 }
 catch(SQLException e){
		 System.out.println(e.toString() + " execQuery() method failed"); 
	 }
 }
 public void CloseConn(){
	 try{
	 conn.close();
	 }catch(SQLException e){
	 System.out.println(e.toString() + " CloseConn() method failed");
	 }
 }
 
	
}


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
1/19/2003 11:54:47 PM

sql connections
(If this comment was disrespectful, please report it.)

 
3/24/2004 1:23:38 AM

this is a good coding congratulation.
how do i write a code that will retried data from the database and print through reports
(If this comment was disrespectful, please report it.)

 
1/27/2005 4:03:40 AM

How do i write a code connect oracle database to retrieved some field and save it in Axapta table?
(If this comment was disrespectful, please report it.)

 
1/27/2005 4:06:07 AM

How to write the code connect to oracle database using ODBC to download some field and save it in Axapta table?
need help!
Thanks!
(If this comment was disrespectful, please report it.)

 
10/12/2005 1:53:47 AMmm

you must try to read ConnectionPool class.
(If this comment was disrespectful, please report it.)

 
5/17/2006 12:55:16 PMzoubair

hi how are you??
congratilation it's was a good code source, caus it's very simple and comprehensible, good continuation
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.