Reputation: 21
Thanks in advance for any help. I am creating a Database - Client application using java in Eclipse. I am using MySQL 5.6 for my database. I have a method to create and return a Connection Object that I will use for querying the database, and a method to return all of the rows in the table as a JSON array. The problem comes in the query method when trying to call the connection method.
at: try{ con.getDBConnection(); its telling me there is an error for getDBConnection(); and the suggestions that it gives is to add cast to 'con'.
and I can't get the query method to compile from the main method.
package binaparts.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.*;
import org.json.JSONArray;
import binaparts.util.ToJSON;
public class returnAllParts extends DBConnect{
public JSONArray queryReturnAllParts() throws Exception{
PreparedStatement query = null;
Connection con = null;
ToJSON converter = new ToJSON();
JSONArray json = new JSONArray();
try{
con.getDBConnection();
query = con.prepareStatement("SELECT * " + "from `parts list`" );
ResultSet rs = query.executeQuery();
json = converter.toJSONArray(rs);
query.close();
}catch(SQLException SQLex){
SQLex.printStackTrace();
}catch(Exception ex){
ex.printStackTrace();
}finally{
if(con != null){
con.close();
}
}
return json;
}
}
dao package code below:
package binaparts.dao;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import com.mysql.jdbc.PreparedStatement;
public class DBConnect {
private Statement st = null;
private ResultSet rs = null;
private Connection con = null;
private PreparedStatement pst = null;
private String serverName = "localhost";
private String portNumber = "3306";
private String dbms = "mysql";
private Object userName = "dwilson";
private Object password = "abc";
public Connection getDBConnection() throws SQLException {
Properties connectionProps = new Properties();
connectionProps.put("user", this.userName);
connectionProps.put("password", this.password);
try{
con = DriverManager.getConnection("jdbc:" + this.dbms + "://" + this.serverName + ":" + this.portNumber + "/", connectionProps);
}catch(Exception ex){
ex.printStackTrace();
con = null;
}finally{
if(con != null){
System.out.println("Connected to database");
}
}
return con;
}
public String getUser(){
try{
DatabaseMetaData dmd = con.getMetaData();
String username = dmd.getUserName();
//System.out.println("Current User: "+username);
return username;
}catch(Exception ex){
System.out.println(ex);
ex.printStackTrace();
return null;
}
}
}
main method below:
public class Main{
public static void main(String[] args){
DBConnect con = new DBConnect();
try {
con.getDBConnection();
System.out.println(con.getUser());
System.out.println(con.queryReturnAllParts());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Login loginGUI = new Login();
//MainFrames m = new MainFrames();
//m.displayGUI();
}
}
The System.out.println(con.getUser());
does work
Upvotes: 0
Views: 3284
Reputation: 85779
In your returnAllParts#queryReturnAllParts
method, change
con.getDBConnection();
By
con = getDBConnection();
The problem is that con
is a variable from java.sql.Connection
and it doesn't have a getDBConnection
method. Since your current returnAllParts
class extends DBConnect
class, it can access to the public Connection getDBConnection
method with no problems.
The System.out.println(con.getUser()); does work
This is because in your Main#main
class, you have declared DBConnect con
. Do not confuse this variable with the con
variable declared in other methods.
Not directly related to the problem, but I suggest you some improvements to your current code/design:
returnAllParts
class for something more meaningful for future readers (even you in some days or weeks will become in a future reader of your code). From reading your code, it looks like this class should be renamed to PartList
.List<PartList>
object and that another layer in your application (probably the closest to presentation) will apply the transformation from your objects to a JSON String.DBConnect
object instead of extending from it. In this way, you could have a single DBConnect
object per database connection configuration associated to all the related DAOs.Upvotes: 3
Reputation: 279880
In this code
PreparedStatement query = null;
Connection con ;
ToJSON converter = new ToJSON();
JSONArray json = new JSONArray();
try{
con.getDBConnection();
the variable con
is of type java.sql.Connection
, not DBConnect
. That type does not have a getDBConnection()
method. I believe you meant to use
con = this.getDBConnection();
in your first returnAllParts
class (which extends DBConnect
). (Please use Java naming conventions.)
Upvotes: 1
Reputation: 3720
In your "returnAllParts" method you define Connection con ;
.
That Connection
is a java.sql.Connection object and it doesn't have the method you seek. Remove it.
Upvotes: 0