I have an android app and trying to connect to my database created in heroku. I keep getting an exception though. I get the sqlexception: no suitable drivers. Here is my code: This is the class where I execute the sql statements and try to make a connection.
public class DB_Connection
{
public static final String DATABASE_URL =
"postgres://iiuoxigyfsvsnd:aI8qkTno1bXZYtB74VovkMo75o@ec2-23-23-22550.compute-1.amazonaws.com:5432/dfpa4aagbgfcd7";
public int value;
// the empty constructor. without this we cant call DB_Connection.getConnection() from another class.
DB_Connection()
{
value = 0;
}
/*
This is the method that opens the actual connection mto the Heroku Server
*/
protected static Connection getConnection() throws URISyntaxException, SQLException
{
URI dbUri = new URI(DATABASE_URL);
String username = dbUri.getUserInfo().split(":")[0];
String password = dbUri.getUserInfo().split(":")[1];
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + dbUri.getPath();
System.err.println(dbUrl);
Properties props = new Properties();
props.setProperty("user", username);
props.setProperty("password", password);
props.setProperty("ssl", "true");
props.setProperty("sslfactory", "org.postgresql.ssl.NonValidatingFactory");
return DriverManager.getConnection(dbUrl, props);
}
public void userLogin(Connection cnct, String userE_Mail, String userPass)
{
String results = new String();
try
{
Statement stmt = cnct.createStatement();
ResultSet rs = stmt.executeQuery("SELECT pass FROM users WHERE e_mail =" + userE_Mail + ";");
while (rs.next())
{
results = rs.getNString("pass");
}
if (userPass == results)
{
value = 1;
}
else
{
results = "Invalid login. Please try again";
}
}
catch (SQLException sqe)
{
System.out.println(sqe.getMessage());
results = sqe.getMessage();
}
finally
{
if (cnct != null)
{
try
{
cnct.close();
}
catch (SQLException e)
{
}
}
}
}
/* TODO: password retrieval? */
public ArrayList getEvents(Connection cnct)
{
ArrayList<DB_Event> eventList = null;
ResultSet rs = null;
try
{
Statement stmt = cnct.createStatement();
rs = stmt.executeQuery("SELECT * FROM events");
// every row returned by the databse is parsed into a DB_event
// and added to an ArrayList<>.
while (rs.next())
{
DB_Event event = new DB_Event(rs.getInt("event_id"),
rs.getNString("event_name"), rs.getDate("date_time"),
rs.getString("location"), rs.getNString("address"),
rs.getString("e-mail"));
eventList.add(event);
}
}
catch (SQLException sqe)
{
//TODO
}
return eventList;
}
}
This is the class I actually try to use it in:
public class NewRegisterScreen extends ActionBarActivity
{
Connection cnct;
EditText number;
EditText password;
EditText fullName;
EditText email;
String firstName, lastName;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_screen);
Button acceptButton = (Button) findViewById(R.id.acceptUserButton);
fullName = (EditText) findViewById(R.id.enterName);
password = (EditText) findViewById(R.id.editPassword);
number = (EditText) findViewById(R.id.enterPhone);
email = (EditText) findViewById(R.id.enterEmail);
// new DB_connection established. Added by Christina
DB_Connection DBcnct = new DB_Connection();
try
{
cnct = DB_Connection.getConnection();
}
catch (URISyntaxException e)
{
System.err.println("Didn't connect");
}
catch (SQLException sqe)
{
sqe.printStackTrace();
System.err.println("Didn't connect. Not sure why");
}
}
I keep hitting the sqlexception and when I click where it is on the printstacktrace, it says the error occurs in the class DB_Connection in the getConnection() method stating no suitable driver was return from that app. Can anyone please tell me why and how to fix it?