Results 1 to 5 of 5
Thread: JDBC class check
- 08-13-2009, 01:16 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 6
- Rep Power
- 0
JDBC class check
Hi,
I have got worked a java class which connects to a database and gets information out. It works fine. My problem is trying to make the code "object oriented" and split it up so I dont have to copy and paste the connection information for each and every class. I simply want a connection class.
I have created the following but is it the right way to go about it?
Any suggestions / tips?
Thanks!
Main Class
Connect ClassJava Code:import java.sql.*; import java.util.Properties; private Statment stmt = null; private ResultSet rs = null; public static void main(String[] args) throws Exception { Connect Financedb = new Connect(); stmt = Financedb.getConnection(con.createStatement()); String SQLStatement = "SELECT * FROM person;"; rs = stmt.executeQuery(SQLStatement); rs.next(); String name = rs.getString("person_name"); System.out.println(name) Financedb.closeConnection(); }
Java Code:import java.sql.*; import java.util.Properties; public class Connect { private static final String dbClassName = "com.mysql.jdbc.Driver"; private static final String connectionurl = "jdbc:mysql://127.0.0.1/finance"; private java.sql.Connection con = null; private java.sql.Connection getConnection() throws ClassNotFoundException,SQLException { Class.forName(dbClassName); Properties p = new Properties(); p.put("user","<user>"); p.put("password","<password>"); con = DriverManager.getConnection(connectionurl,p); return con } private void closeConnection() { if(con!=null) { con.close(); } con=null; } }
- 08-13-2009, 07:46 PM #2
Member
- Join Date
- Aug 2009
- Posts
- 6
- Rep Power
- 0
Update
I have managed to work it out to the following but I get a NullPointerException error. If Any one had any ideas or suggestions then please let me know!
Error
Connector ClassJava Code:run: java.lang.NullPointerException at connect.getResults(connect.java:45) at Finance.main(Finance.java:13) Exception in thread "main" java.lang.NullPointerException at Finance.main(Finance.java:15) Java Result: 1 BUILD SUCCESSFUL (total time: 1 second)
Main ClassJava Code:import java.sql.*; import java.util.Properties; public class connect{ private static final String dbClassName = "com.mysql.jdbc.Driver"; private static final String connectionurl = "jdbc:mysql://127.0.0.1/finance"; private static Connection con = null; private static Statement stmt = null; private static ResultSet rs = null; private Properties p = new Properties(); // Constructor public connect() { } private java.sql.Connection getConnection() { try { p.put("user",<user>); p.put("password",<password>); Class.forName(dbClassName); con = DriverManager.getConnection(connectionurl,p); if(con!=null) System.out.println("Connection Successful!"); } catch(Exception e) { e.printStackTrace(); System.out.println("Error Trace in getConnection() : " + e.getMessage()); } return con; } public java.sql.ResultSet getResults() { try { String SQLStatement = "SELECT * FROM person;"; rs = stmt.executeQuery(SQLStatement); } catch(Exception e) { e.printStackTrace(); } closeConnection(); return rs; } public void displayDbProperties() { java.sql.DatabaseMetaData dm = null; java.sql.ResultSet rs = null; try { con= this.getConnection(); if(con!=null) { dm = con.getMetaData(); System.out.println("Driver Information"); System.out.println("\tDriver Name: "+ dm.getDriverName()); System.out.println("\tDriver Version: "+ dm.getDriverVersion ()); System.out.println("\nDatabase Information "); System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName()); System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion()); System.out.println("Avalilable Catalogs "); rs = dm.getCatalogs(); while(rs.next()) { System.out.println("\tcatalog: "+ rs.getString(1)); } rs.close(); rs = null; closeConnection(); } else System.out.println("Error: No active Connection"); } catch(Exception e) { e.printStackTrace(); } dm=null; } private void closeConnection() { try { if(con!=null) con.close(); con=null; } catch(Exception e) { e.printStackTrace(); } } }
Java Code:import java.sql.*; import java.util.Properties; public class Finance { private static ResultSet rs = null; public static void main(String[] args) throws Exception { connect myDbTest = new connect(); rs = myDbTest.getResults(); rs.next(); String name = rs.getString("person_name"); System.out.println(name); } }Last edited by DavidBainbridge; 08-13-2009 at 07:57 PM. Reason: Removed personal information opps!
- 08-13-2009, 07:56 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 6
- Rep Power
- 0
[Solved]
Never mind I solved it myself! For anyone wondering the correct code is:
Connection Class
Main ClassJava Code:import java.sql.*; import java.util.Properties; public class connect{ private static final String dbClassName = "com.mysql.jdbc.Driver"; private static final String connectionurl = "jdbc:mysql://127.0.0.1/finance"; private static Connection con = null; private static Statement stmt = null; private static ResultSet rs = null; private Properties p = new Properties(); // Constructor public connect() { } private java.sql.Connection getConnection() { try { p.put("user",<user>); p.put("password",<password>); Class.forName(dbClassName); con = DriverManager.getConnection(connectionurl,p); if(con!=null) System.out.println("Connection Successful!"); } catch(Exception e) { e.printStackTrace(); System.out.println("Error Trace in getConnection() : " + e.getMessage()); } return con; } public java.sql.ResultSet getResults() { try { con= this.getConnection(); stmt = con.createStatement(); String SQLStatement = "SELECT * FROM person;"; rs = stmt.executeQuery(SQLStatement); } catch(Exception e) { e.printStackTrace(); } return rs; } public void displayDbProperties() { java.sql.DatabaseMetaData dm = null; java.sql.ResultSet rs = null; try { con= this.getConnection(); if(con!=null) { dm = con.getMetaData(); System.out.println("Driver Information"); System.out.println("\tDriver Name: "+ dm.getDriverName()); System.out.println("\tDriver Version: "+ dm.getDriverVersion ()); System.out.println("\nDatabase Information "); System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName()); System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion()); System.out.println("Avalilable Catalogs "); rs = dm.getCatalogs(); while(rs.next()) { System.out.println("\tcatalog: "+ rs.getString(1)); } rs.close(); rs = null; closeConnection(); } else System.out.println("Error: No active Connection"); } catch(Exception e) { e.printStackTrace(); } dm=null; } private void closeConnection() { try { if(con!=null) con.close(); con=null; } catch(Exception e) { e.printStackTrace(); } } }
Java Code:import java.sql.*; public class Finance { private static ResultSet rs = null; public static void main(String[] args) throws Exception { connect myDbTest = new connect(); rs = myDbTest.getResults(); rs.next(); String name = rs.getString("person_name"); System.out.println(name); } }
- 08-13-2009, 08:24 PM #4
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
Looks pretty good. You could try encapsulating all sql stuff into the connection class and having the methods returning your own bean class as well, instead of accessing your ResultSet outside. Not better or required or necessarily advised, but it does provide a little more practice in making things "object oriented"
- 08-17-2009, 12:52 PM #5
Member
- Join Date
- Aug 2009
- Posts
- 6
- Rep Power
- 0
Whats the best way of going about this?Looks pretty good. You could try encapsulating all sql stuff into the connection class and having the methods returning your own bean class as well, instead of accessing your ResultSet outside. Not better or required or necessarily advised, but it does provide a little more practice in making things "object oriented"
My only concern would be that the recordset data is different for each select call.
What would I return from the connection class? If its raw data (strings and ints etc) I would need to have a connect method for each data call?!?
Is there an easier way to do it?
Similar Threads
-
Check box tag
By elizaabru in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 08-26-2008, 02:37 PM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By Java Tip in forum Java TipReplies: 0Last Post: 04-01-2008, 10:17 AM -
failure at Class.forName("oracle.jdbc.driver.OracleDriver");
By RonNYC in forum EclipseReplies: 1Last Post: 03-14-2008, 02:51 PM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 12:56 PM -
Security Violation:attempt to use Restricted Class: sun.jdbc.odbc.JdbcOdbcDriver
By Heather in forum Advanced JavaReplies: 1Last Post: 07-14-2007, 05:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks