我有一个Android应用程序,并尝试连接到我在heroku中创建的数据库。我不断得到一个例外。我得到了sqlexception:没有合适的驱动程序。 这是我的代码: 这是我执行sql语句并尝试建立连接的类。
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)
{
}
}
}
}
/ * 去做: 取回密码? * /
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;
}
}
这是我实际尝试使用它的类:
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");
}
}
我继续点击sqlexception,当我点击printstacktrace上的位置时,它表示在getConnection()方法的类DB_Connection中发生错误,表明没有合适的驱动程序从该应用程序返回。任何人都可以告诉我为什么以及如何解决它?