stumped by "[Microsoft][ODBC Driver Manager] Invalid cursor state" [duplicate] - java

i ran select command and printed the result in system.out using below code. was getting expected result with invalid cursor error.
could you please any one tell, why this error was occurred after printing the expected result and how to fix it?
code:
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "path";
conn = DriverManager.getConnection(url);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
String select="SELECT DISTINCT col1,col2 FROM Tablename";
ResultSet rs = stmt.executeQuery(select);
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
String columnValue;
while (rs.next())
{
for (int i = 1; i <= columnsNumber; i++) {
columnValue= rs.getString(i);
System.out.print(columnValue+" ");
}
}
}
catch(SQLException exc){
exc.printStackTrace();
}
output:
test1 result1
test2 result2
test3 result3
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6964)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7121)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:3914)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:5697)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:353)

I was able to recreate your issue. It appears to be an "unfortunate behaviour" of the JDBC-ODBC Bridge and the Access ODBC driver when working with SELECT DISTINCT ... queries and ResultSet.TYPE_SCROLL_SENSITIVE.
The following kluge seems to work around the issue for me:
String select="SELECT * FROM (SELECT DISTINCT FirstName,LastName FROM Clients)";
Switching from ResultSet.TYPE_SCROLL_SENSITIVE to ResultSet.TYPE_FORWARD_ONLY also appears to avoid the issue.
However, since the JDBC-ODBC Bridge is obsolete and has been removed from Java 8 you might consider using the UCanAccess JDBC driver instead. For more details see
Manipulating an Access database from Java without ODBC

Related

Calling PL-SQL Procedure over database link from Java

here my objective is to call a procedure over a database link in java. procedure takes one input and has got cursor as an output.
to check if my code is working properly, I created dummy procedure in my database and tried executing. it is working, able to get cursor and play with it.
however when i am calling some procedure over database link, getting error as
java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00201: identifier 'HR_CLICK_GET_EMP_DETAILS#IBSLUAT1.WORLD' must be declared
I had a call with developer who had created these procedure. according to him procedures exist at this end and access is already given to my user.
Now my questions and queries are
is there something different, i have to do while calling a procedure over database link(code is below)
what are the things i should be asking to sql developer. by the way database link is right.
String prc_name = "HR_CLICK_GET_CM_AND_ABOVE#IBSLUAT1.WORLD(?,?)";
String runSP = "{ call "+prc_name+" }";
String runSP1 = "{ call get_user_by_userId(?,?) }"; this one is working
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:#xx.xx.xxx.xx:port:SERVICE", "username", "password"); // uat
CallableStatement cs = conn.prepareCall(runSP);
cs.setString(1, "705151");
cs.registerOutParameter(2, OracleTypes.CURSOR);
cs.execute();
// get refcursor and convert it to ResultSet
ResultSet resultSet = (ResultSet) cs.getObject(2);
ResultSetMetaData rsmd = resultSet.getMetaData();
int columnCount = rsmd.getColumnCount();
System.out.println("Total Columns in ResultSet : "+columnCount);
System.out.println("Now Analyzing column one by one:\n\n-----------------------------------------------");
for (int i = 1; i <= columnCount; i++ ) {
String name = rsmd.getColumnName(i);
System.out.println("Column No:"+i+">>>>>>>>"+name);
}
}
catch(SQLException s)
{
s.printStackTrace();
}
catch(ClassNotFoundException s)
{
s.printStackTrace();
}
thanks in advance
Ashish
Try to use the Oracle user's name who owns the procedure as a prefix:
username.HR_CLICK_GET_EMP_DETAILS#IBSLUAT1.WORLD
Answer is "synonym".
SQL Developer has created a synonym for HR_CLICK_GET_EMP_DETAILS as HR_CLICK_GET_EMP_DETAILS#IBSLUAT1.WORLD
that is what he informed me, I could not quite wrap my head around that but able to hit the procedure.
but now , able to get get the metadata of a result set but unable to traverse rows.
I'm getting an error:
java.sql.SQLException: ORA-24338: statement handle not executed
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:207)
at oracle.jdbc.driver.T4CStatement.fetch(T4CStatement.java:1018)
at oracle.jdbc.driver.OracleResultSetImpl.close_or_fetch_from_next(OracleResultSetImpl.java:291)
at oracle.jdbc.driver.OracleResultSetImpl.next(OracleResultSetImpl.java:213)
at ashishtest.StoredProcedureCursor.main(StoredProcedureCursor.java:80)
I guess, new forum is required for this error.
also I am not marking this as solved as not sure how solution works.

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);
}

Warning: Looking for usage map at page 1774, but page type is 1 in UCanAccess

When trying to run a program that interfaces with Access 2010 it throws the error
WARNING:Looking for usage map at page 1774, but page type is 1
and then proceeds to throw errors about user lacks privilege to access the table I'm trying to use.
This program seems to work perfectly fine when using Access 2013, and it worked once on Access 2010 when I tried the update statement for the first time. Now it doesn't work at all.
I can't seem to find any reference to this error anywhere online, so I'm hoping someone else has encountered it before.
It throws an error on this line of code, which it doesn't do when interfacing with Access 2013:
ResultSet rSet = stmt.executeQuery("Select * FROM Players");
The entire method is:
public int addPlayer(String name, int x) throws SQLException //drafts a person to team x (ownerID)
, ClassNotFoundException
{
//Database db = new DatabaseBuilder().setCodecProvider(new CryptCodecProvider()).open(new File("BBFBLMasterVersion3.accdb"));
Connection con;
try
{
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
con = DriverManager.getConnection("jdbc:ucanaccess://C:/Users/Andrew/Dropbox/Public/Schoolwork/IRC/BBFBLMasterVersion3.accdb"); //name of ODBC driver
Statement stmt = con.createStatement();
//stmt.executeQuery("SELECT * FROM DraftNightQuery");
//ResultSet rSet = stmt.getResultSet();
ResultSet rSet = stmt.executeQuery("Select * FROM Players");
String[] split = name.split(" ");
String salary = "1";
while(rSet.next())
{
String lastName = rSet.getString("Last");
//int x = Integer.parseInt(salary);
if(split[0].toLowerCase().equalsIgnoreCase(lastName))
{
String firstName = rSet.getString("First"); //get the item from column named Team Name
if(split[1].toLowerCase().equalsIgnoreCase(firstName))
{
Statement connec = con.createStatement();
Statement idMatch = con.createStatement();
String id = rSet.getString("ID");
connec.executeUpdate("UPDATE Players SET OwnerID = "+x+" WHERE Last ='"+split[0]+"' AND First='"+split[1]+"' ");
//stmt.executeUpdate(whoToAdd);
ResultSet temp =idMatch.executeQuery("SELECT * FROM Salaries WHERE ID ='"+id+"'");
while(temp.next()){
String tempID = rSet.getString("ID");
if(id.toLowerCase().equalsIgnoreCase(tempID)){
salary = temp.getString("Salary");
}
}
con.close();
connec.close();
stmt.close();
idMatch.close();
return Integer.parseInt(salary);
}
}
}
return 1;
}
finally{}
}
As Gord said, the problem is detected and logged by jackcess and it's likely caused by a accdb file corruption (I would try to fix it with the Compact and repair Access tool). Also, it may be in turn caused by its use in a sync dropbox folder. Thus, that the file was modified via UCanAccess or in another way is irrelevant, because all happened at a lower layer. Please, find similar issues reported in the jackcess forum (yes, they are googlable, did you remove the page number?)

Can't read tables from firefox using Java sqlite jdbc

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);

Categories

Resources