Can't read tables from firefox using Java sqlite jdbc - java

I'm using the following code to connect and read moz_bookmarks from Java
String connection = "jdbc:sqlite:/" + Tracking.FILES_LOCATION + "places.sqlite";
Connection conn1 = DriverManager.getConnection("jdbc:sqlite:path_to_places.sqlite/");
Statement stat = conn1.createStatement();
ResultSet rs1 = stat.executeQuery("select * from moz_bookmarks;");
But I keep getting : "java.sql.SQLException: file is encrypted or is not a database"
Even after upgrading my jdbc sqlite driver for sqlite3
Any help is greatly appreciated
Thanks!

You create a connection name in first line, but in the second line you have a connection string which is probably copied from some tutorial.
Try to use connection variable when calling getConnection method.

Try this:
String connection = "jdbc:sqlite:/" + Tracking.FILES_LOCATION + "places.sqlite";
Connection conn1 = DriverManager.getConnection(connection);
Statement stat = conn1.createStatement();
String Query = "select * from moz_bookmarks";
ResultSet rs1 = stat.executeQuery(Query);

Related

How to use fixed length CCSID 37 char value in where clause of sql select for IBM DB2 iSeries in node.js

My application is connected to IBM DB2 iSeries database connected through DataDirect approach using db2.jar in Java. When I run any select query with specific data in where clause it do not detect the value that I pass in where condition. Data type for that column is of CHAR(7) and I am passing a value as 'P544901'. How can I use this or any other 7 char value and db can detect it. Is there any other process that can solve my problem.
Sample Code in Java -
String sql = "SELECT poMast.ORDNO from AMFLIBL.POMAST AS poMast WHERE poMast.ORDNO = ? ";
Class.forName("com.ddtek.jdbc.db2.DB2Driver");
String url = "jdbc:datadirect:db2://hostname:port;DatabaseName=dbName;";
Connection con = DriverManager.getConnection(url, "username","password");
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, 'P544901');
ResultSet rs = preparedStatement.executeQuery();
System.out.println("ResultSet : \n");
System.out.println(" VNDNO");
while (rs.next())
{
System.out.println(rs.getString("ORDNO"));
}
try to modify like this (replace simple quote by double quote)
preparedStatement.setString(1, "P544901");

how can i connect android application to mysql using jsp?

I want to use jsp rather than php. Is it by jdbc driver? And what are the steps.
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://<server>/<database>",
<username>,
<password>);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT access_name, access_code FROM user_login");
while (rs.next()) {
x = rs.getString("access_name");
y = rs.getString("access_code");
}
This code gives me an exception like ClassNotFoundException com.mysql.jdbc.driver
You should download MySQL Connector from this link :
https://dev.mysql.com/downloads/connector/j/5.0.html .
Then just add it to your classpath.

multiple sql command

I'm trying to execute multiple sql commands, but it gives me "error in your SQL syntax;"
Db_Connection dbconn = new Db_Connection();
Connection myconnection = dbconn.Connection();
String sqlString = "SELECT DISTINCT std_id FROM std_crs WHERE crs_id ='222123'; "
+ "SELECT * FROM cplus_grades ;";
Statement myStatement = myconnection.createStatement();
boolean results = myStatement.execute(sqlString);
do {
if (results) {
ResultSet rs = myStatement.getResultSet();
while (rs.next()) {
}
rs.close();
}
results = myStatement.getMoreResults();
} while(results);
myStatement.close();
I did a small test with three JDBC drivers:
MS SQL: works, returns two result sets
MySQL: fails with a syntax error - that is what you are seeing
HSQLDB: runs, but returns only one result set.
So I guess it simply depends on the JDBC driver if this technique works. Maybe it works only in MS SQL JDBC.
UPDATE:
It also works with Postgres.
please
1. String dbUrl = "jdbc:mysql://yourDatabase?allowMultiQueries=true";
this should be your jdbc connection url

How to list all tables' name in MSAccess database file using sql query in java?

I've getting some code here
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + f.getPath() + ";";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(url);
Statement st=conn.createStatement();
String query="SELECT name FROM MSysObjects WHERE Type=1 AND Flags=0";
ResultSet rs=st.executeQuery(query);
ArrayList<String> tableNames=new ArrayList<String>();
while(rs.next()){
String name=rs.getString(1);
tableNames.add(name);
}
but there has some problem
[Microsoft][ODBC Microsoft Access Driver] Record(s) cannot be read; no read permission on 'MSysObjects'."
and I searched for this problem and Just found this: social.msdn.microsoft.com/Forums/sqlserver/en-US/… "
Because MSysObjects is a system table in Access, the Admin user does not have permission to read data in it".
I read the answer, but in fact, I want to access permission by programming, does anyone can help me? Thank you very much
Seems that Access does not allow to query MSysObjects. Did you follow the steps in the article to give your user the adequate permission?
Anyway JDBC also has an own API which allows you to read database metadata:
Connection conn = ...
DatabaseMetaData metaData = conn.getMetaData();
ResultSet rs = metaData.getTables(null, null, "%", null);
while (rs.next()) {
String name = rs.getString(3); // see javadoc of DatabaseMetaData
tableNames.add(name);
}

No Database selected when retrieving from mysql website

I have a mysql database that I am trying to retrieve from our website host (godaddy). I followed a format that seems to be right but it tells me that:
java.sql.SQLException: No database selected
Code:
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// STEP 2: Register JDBC driver
Class.forName(JDBC_DRIVER);
// STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM item_master";
ResultSet rs = stmt.executeQuery(sql); //<-- This is where the error is.
// STEP 5: Extract data from result set
while (rs.next()) {
// Retrieve by column name
int id = rs.getInt("id");
// Display values
System.out.print("ID: " + id);
}
...
}
I did a print statement of the conn to maybe think the connection could of been null and was shown this:
com.mysql.jdbc.JDBC4Connection#2a6*****
Anyone have any ideas what could cause something like this?
Your URL for your database should include your database name. This is normally your URL followed by a "/DBNAME".
String URL = "jdbc:mysql://localhost:3306/mydb";
Where "mydb" is your database name.
What is the value of DB_URL? As far as I am aware, the URL needs to be of the form:
"WEBURL/DATABASENAME:PORT"
Are you just trying to connect to the WEBURL, without specifying a DATABASE?
I had the same problem.
Just run one more query (before your 'SELECT' statement) to connect to your database.
First:
sql = "USE <yourdatabase>";
ResultSet rs = stmt.executeQuery(sql);
And after:
sql = "SELECT * FROM item_master";
rs = stmt.executeQuery(sql);

Categories

Resources