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