Excel DB: Operation must use an updateable query - java

for a jdbc program, im required to make a connection an excel database. the connection is successfully made but wen entering values into it, its giving an "Operation must use an updateable query" exception.
here s the code:
String url="jdbc:odbc:Sample"; //CHANGE THE DATABASE NAME
Connection conn= DriverManager.getConnection(url,"","");
PreparedStatement prepstat = null;
String insert="INSERT INTO [Sheet1$] ([AccountID], [ProjectID], [PositionID]) VALUES (?,?,?)";
prepstat= conn.prepareStatement(insert);
prepstat.setString(1, accountID);
prepstat.setString(2, projectID);
prepstat.setString(3, positionID);
prepstat.executeUpdate(); // this is where the exception occurs

Did you specifically state that the connection was readwrite in your connection string?
I am not familiar with JDBC, but ODBC would be:
"Driver={Microsoft Excel Driver (*.xls)};" & _
"DBQ=C:\MyFolder\MyWorkbook.xls; ReadOnly=False;"
Excel is read-only by default: http://support.microsoft.com/kb/257819

It was just needed to uncheck the read only while creating the DSN.

Related

User and Session info from within JAVA UDF Oracle 11g

I wish to get user info just like provided by
SELECT SYS_CONTEXT ('USERENV', 'SESSION_USER') FROM DUAL;
and
SELECT SYS_CONTEXT ('USERENV', 'OS_USER') FROM DUAL;
inside a JAVA UDF for Oracle 11g without making a JDBC connection and running these queries to query from DUAL.
I tried System.getProperty("user.name") to read the current OS_user through jvm but I think we are not allowed to fetch information outside the database environment.
More generically, problem statement is to fetch information about the user who has logged into database and using that java UDF (where we need to determine these information) ?
I have found solution to above problem by using the "jdbc:default:connection" which is an internal connection maintained by database itself which is always available. Notice I did not do conn.close(); in the end because this is a shared stream which once closed is closed for all database clients.
public static String doSQL() throws SQLException {
String result = new String();
String q1 = "SELECT SYS_CONTEXT('USERENV','SESSION_USER') FROM DUAL";
Connection conn =
DriverManager.getConnection("jdbc:default:connection");
PreparedStatement ps = conn.prepareStatement(
q1
);
ResultSet rs = ps.executeQuery();
while (rs.next())
result = rs.getString(1);
return "my udf says"+result;
}

To fetch database objects and data for other DB than default

We are working on Sybase database using Java JDBC. We are fetching some data and data objects from Sybase. Below code is used to get data :
Connection con = DriverManager.getConnection(sDBUrl, sUserName, sPassword);
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(myQuery);
DriverManager.getConnection create and get connection for database url, user name and password given. Database Url format is : 192.123.111.15:5000?ServiceName=DATABASENAME
BUT there is no effect of database name given in getConnection, it always return database objects and its data for DEFAULT database set for the user.
What we need is : We need to get database objects and data from database name specified in DB url NOT default one.
How can we achieve this?

JDBC PreparedStatement with "?" in the column name

I am using a JDBC connection to fetch data from an Access database.
The database design is not my control. In the database there are columns that have "?" included in their names, for example: Open?, Paid?, and lots more.
When I try to fetch data with a PreparedStatement it gives me an error. The query is:
SELECT Open? FROM tblJobList WHERE WeekEnding=?
I also tried to use brackets like [Open?], but the result is the same.
The error I receive is "Too few parameters ..." as I am pushing only one parameter into the PreparedStatement.
I can not use normal statement because of WeekEnding=? as this value is a Timestamp and I could not manage to work it with Statement. Only prepared statement works here.
Can anyone tell me how to use these kind of column names in a PreparedStatement?
use the " character
"SELECT \"Open?\" FROM tblJobList WHERE WeekEnding=?"
tested this against oracle and appears to work with mssqlserver
How to select a column in SQL Server with a special character in the column name?
Just to update this for current technologies:
While the JDBC-ODBC Bridge and Access ODBC were unable to handle a PreparedStatement with a column name containing a question mark, the UCanAccess JDBC driver handles it just fine, as can be confirmed with the following code:
String connectionUrl = "jdbc:ucanaccess://C:/Users/Public/UCanAccessTest.accdb";
Connection conn = DriverManager.getConnection(connectionUrl);
String sql = "SELECT ID, [Open?] FROM tblJobList WHERE WeekEnding=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setDate(1, java.sql.Date.valueOf("2016-01-01"));
ResultSet rs = ps.executeQuery();
while (rs.next()) {
System.out.printf("%d: %s%n", rs.getInt("ID"), rs.getBoolean("Open?"));
}
conn.close();
For more information on UCanAccess, see
Manipulating an Access database from Java without ODBC
I am not sure but you can try // to escape the special meaning of ? and to use it as a normal character. Like:
"SELECT Open//? FROM tblJobList WHERE WeekEnding=?"
You can get something similar to your problem here:
Round bracket in string with JDBC prepared statement
Escaping quotes in MSSQL is done by a double quote, so a '' or a "" will produce one escaped ' and ", respectively.

how the command 'use database-name' is using java code

I would like to make o program that would store data from excel files in databases. I have plenty of databases so in my program i have to choose in which one I will store the data.
I have made the code to be able to connect mysql with my program and to show the available databases. What I would like to do now is to say in which database i would store the data.
To be more specific I would like the user first of all to see tha available databases in his client and afterwards he would have the chance to say in which database the data would be stored.
Could anyone help me how I would do this?
The code to see all the available databases is the below:
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection(
"jdbc:mysql://localhost:3306/", "root", "root");
DatabaseMetaData meta = (DatabaseMetaData) con.getMetaData();
ResultSet res = meta.getCatalogs();
System.out.println("List of the databases: ");
while (res.next()){
System.out.println (" " +res.getString(1));
}
Thank you in advance!
I hope this SO link should help you . You can get all the data from the connection object
First do a create a simple connection
Connection con = (Connection) DriverManager.getConnection( "jdbc:mysql://localhost:3306/", "root", "root");
Look for an example here
see how he uses ResultSet Class to access the result.
Now,
retrieve information from information_schema.SCHEMATA so you can query like
SELECT schema_name FROM information_schema.SCHEMATA S;
to get all the schemas
Next after getting choice from user(maybe from console) you can set database according to uservalue using Connection#setCatalog()
If you want table information use this query
SELECT * FROM information_schema.TABLES T;
It would list all the tables in all schemas

Java: insert accented characters in mysql

If I have this query from java:
String query="insert into user (..., name, ...) values (..., 'à', ...)";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/Spinning?user=root");
PreparedStatement prest = con.prepareStatement(query);
prest.executeUpdate();
In the db I will have a strange character: a diamond with a question mark inside.
Is there any solution to this problem?
Change your connection url to the following:
jdbc:mysql://localhost/Spinning?user=root&useUnicode=true&characterEncoding=utf8
Verify the character set you are using in MySQL DB. You can try "SHOW CREATE TABLE xxxx" to print the table DDL with charset being used.
Verify the character set you are using in JDBC driver. If using MySQL ConnectorJ, you can set charset in the JDBC url.

Categories

Resources