Android sqlite jdbc can't connect because of Read-only file system - java

I'm trying to open an sqlite jdbc connection but I keep getting an exception
java.sql.SQLException: opening db:
'\data\data\com.lppapp.ioi.lpp\databases\lppDB.db': Read-only file
system
Before trying to get the jdbc connection I copy the database into the internal storage and try to access it from there. I'm using this code to connect:
public Connection connect() {
String url = "jdbc:sqlite:\\data\\data\\com.lppapp.ioi.lpp\\databases\\lppDB.db";
Connection conn = null;
try {
Properties config = new Properties();
config.setProperty("open_mode", "1");
//TODO: can't open db (read-only file system) even though directory has a+rw permissions
conn = DriverManager.getConnection(url, config);
System.out.println("Connection successful.");
} catch (SQLException e) {
System.out.println("Connection failed.");
System.out.println(e.getMessage());
}
return conn;
}
I'm using an Android emulator and I've checked the permissions of the databases directory.
I've tried every solution found on the web but with no help.

I've decided to use Android's native SQLite API as mentioned by CommonsWare.
Thanks!

Related

Using eclipse to call database from Google Cloud SQL

I m trying to create a dynamic website where I can get data from the database and output it to the user.
I created the connection between MySQL workbench to Google cloud app engine, and I run on the cloud shell to test the database, it works.
But when I try to change the URL on eclipse, it just cannot connect to the server, and gives me errors "Failed to load resource: the server responded with a status of 500 ()".
the URL sting are:
String url = "jdbc:mysql:///film?cloudSqlInstance="
+ "causal-relic-333309:europe-west6:jiayaodb="
+ "&socketFactory = com.google.cloud.sql.mysql.SocketFactory"
+ "&user=root"
+ "&password=password";
private Connection openConnection(){
// loading jdbc driver for mysql
try{
Class.forName("com.mysql.jdbc.Driver");
} catch(Exception e) { System.out.println(e); }
// connecting to database
try{
// connection string for demos database, username demos, password demos
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
} catch(SQLException se) { System.out.println(se); }
return conn;
}
So I think is the connection error where I can't get it to connect to Google Cloud SQL.
you need to use Cloud SQL proxy to connect to the database from your local machine.

JDBC connection not happening [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 7 years ago.
Below is the code I used for jdbc connection
String dbUrl="jdbc:mysql://localhost:3306/mysql";
String user= "kumar";
String pwd="ratiol";
try (Connection connection = DriverManager.getConnection(dbUrl, user, pwd)) {
System.out.println("Database connected!");
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
but I got error as below-
Exception in thread "main" java.lang.IllegalStateException: Cannot connect the database!
at jdbcConnection.Jdbcdemo.main(Jdbcdemo.java:22)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mysql
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at jdbcConnection.Jdbcdemo.main(Jdbcdemo.java:19)
Can you please tell me how can I get jdbc url?
I am using eclipse(mars) in ubuntu 14.04
if you are using netbeans, right click project -->properties -->libraries-->add library and select MySQL JDBC Driver
just add the com.mysql.jdbc.Driver to the lib folder in you program files.
This is what you wrote
try (Connection connection = DriverManager.getConnection(dbUrl, user, pwd)) {
System.out.println("Database connected!");
} catch (SQLException e) {
throw new IllegalStateException("Cannot connect the database!", e);
}
it would be like this:
Connection con = null;
try {
//registering the jdbc driver here, your string to use
//here depends on what driver you are using.
Class.forName("something.jdbc.driver.YourFubarDriver");
Connection connection = DriverManager.getConnection(dbUrl, user, pwd)
} catch (SQLException e) {
throw new RuntimeException(e);
}
Please check Class.forName not more needed when using JDBC v.4
Let's take a quick look at how we can use this new feature to load a
JDBC driver manager. The following listing shows the sample code that
we typically use to load the JDBC driver. Let's assume that we need to
connect to an Apache Derby database, since we will be using this in
the sample application explained later in the article:
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
Connection conn =
DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
But in JDBC 4.0, we don't need the Class.forName() line. We can
simply call getConnection() to get the database connection.
Note that this is for getting a database connection in stand-alone
mode. If you are using some type of database connection pool to manage
connections, then the code would be different.
There are plenty of reason for the exception No suitable driver found for jdbc:mysql like
Your JDBC URL can be wrong.
ClassPath/BuildPath/Lib folder missing the connector jar.
Driver Information is Wrong.
Just load the driver first:
Class.forName("com.mysql.jdbc.driver");

What might be wrong with this JSP

conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/ebookshop", "root", "");
XAMPP is hosting MySQL on 3306
I linked the mysql connect jar
ebookshop is the name of a DB
http://i.imgur.com/9XJjLiX.png
I have a user root wit no password
But I am getting a
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/ebookshop
...
I have run other JSP and it works, I have a print statement just before the conn statement and it prints, one after that does not.
Thank you
You need to add the MySQL JDBC driver to your app's classpath. The most straight forward way to do that if you're running in a servlet container such as Tomcat or Jetty, would be to place the driver jar file in your app's WEB-INF/lib folder.
Try this code :-
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception e)
{
System.out.println("Error in Data base Driver:"+e);
}
try
{
String url="jdbc:mysql://localhost:3306/ebookshop", "root", "";
connection=DriverManager.getConnection(url,userName,pass);
}
catch (Exception e)
{
System.out.println("Error in database connection:"+e);
}
First register your driver. Then you can use the url.

How to create a JDBC MySQL connection pool in Tomcat7 installed on Amazon EC2?

I am new and any help will be highly appreciated. I have a Webapp installed on Amazon EC2. I have installed Tomcat7 and MySQL 5.5 on Amazon EC2.
I am using following code in servlet for JDBC-MySQL connection
Connection connection;
Statement statement;
ResultSet rs = null;
try {
// load Connector/J
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(JDBC_MYSQL_STRING);
System.out.println(TAG + " Connection with MySQL established");
statement = connection.createStatement();
....
....
....
rs.close();
statement.close();
connection.close();
} catch (ClassNotFoundException ex) {
Log.log(Level.SEVERE, "ClassNotFoundException", ex);
} catch (SQLException e) {
for (Throwable t : e) {
System.out.println(t.getMessage());
}
I have included mysql-connector-java-5.1.13-bin.jar for Connector/J in the Library folder for Netabeans and deployed war files on tomcat7 in Amazon ec2.
My Question
I have learned that JDBC-MySQL connection is very time consuming operation and connection pools can be used to keep live connection which will reduce time consumed in connection. I am new to this and have read my blogs but unable to understand how to set up connection pool and how to use it in the servlets?
Thanks.
You need to setup the DB Connection in server.xml
Follow this:
http://www.mulesoft.com/tomcat-mysql
The URL of your database would take this form:
"jdbc:mysql://yourdatabasename.foo.us-east-1.rds.amazonaws.com:3306/"

connecting to remote MySQL server using JDBC issue

I am working on a dynamic webapp on eclipse and is trying to access a remote MySQL database. I made sure that all the information is correct. However I can't seem to connect to it. Here's my getConnection method:
public static Connection getConnection() throws SQLException {
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://50.56.81.42:3306/GUEST_BOOK";
String user = "username";
String password = "password";
conn = DriverManager.getConnection(url, user, password);
System.out.println("CONNECTED");
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(InstantiationException e){
e.printStackTrace();
}catch(IllegalAccessException e){
e.printStackTrace();
}
return conn;
}
I am trying to see what the error is but because this is a web app, I can't see that system.out.println anywhere, so it's kind of hard for me to debug with this. Any suggestions on how to debug?
Is your MySQL server accessible from out of the server? I am asking because it is possible to disable external connection for MySQL server, more info is here.
Your credentials are OK (at least its format).
Otherwise, you create Class.forName("com.mysql.jdbc.Driver").newInstance(); but you don't assign it to conn variable.
In web applications, System.out prints to the server's logs file. If you are using tomcat, see logs directory inside tomcat's base directory.
If you are still struggling to see the System out in logs, try to test the class from standalone java class, you will clearly see what the problem is.

Categories

Resources