I am trying to make an app that changes certain values of an MS-Access database. I am not trying to add new lines or anything. My problem is that I get a net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::5.0.0-SNAPSHOT attempt to assign to non-updatable column error. The current code I'm using is
try {
sql = "SELECT * FROM MtnRoads";
Connection connection = DriverManager.getConnection("jdbc:ucanaccess://C://Users//anyGenericProgrammer//Documents//Database1.accdb");
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery(sql);
result.updateString(aNumber, aString);
} catch (Exception e) {
errCode.setText(e.toString());
System.out.println(e);
}`
I have looked at this StackOverflow question to figure out how to even update the lines in the first place, however the example that is used in extremely confusing. Is there any way to make this work without throwing errors? (I am using javax.swing.JFrame library, errCode is a JLable.)
Related
I am still quite new to the world of java. I am working on my second application which is a program that mass updates a time field in my company's SQL database. I am able to run queries through java, and store each query line in a resultset just fine. The thing is that each line of the result set is an update statement. I want to then run those resultset lines. However over and over I keep getting the "SQL command not properly ended" error message when I know full well these statements are formatted correctly and run just fine in TOAD for oracle. Can anyone help me understand whats going on here? I have also tried batching and continue to get the same error.
This is an example of one of the output lines of my query with table and field names changed.
Update sometable.somefield set COMPLETED_TS ='31-OCT-17 06.00.00.000000000 AM'Where eqact_id ='2559340';
Below you can see the end of my SQL string and my runScript2() method.
"\r\n" +
"\r\n" +
"where \"Center\" = S.CODE and S.TIMEZONE_ID = T.ID"; //This String is named SQL1
public void runScript2(){
try {
PreparedStatement statement0 = Connection1.conn.prepareStatement(SQL1);
ResultSet result0 = statement0.executeQuery();
Connection1.conn.setAutoCommit(false);
while(result0.next()) {
PreparedStatement statementq1=Connection1.conn.prepareStatement(result0.getString(1));
statementq1.executeUpdate();
}
Connection1.conn.commit();
}catch (SQLException e1) {
e1.printStackTrace();
}
}
Well I am angry and happy at the same time as I figured out that the issue was that my result0.getString(1) lines had a semicolon at the end of each and for some reason Java didn't like this. They run just fine without this.
Live and you learn I guess.
I am working on an JAVA app that evaluates the data and log sizes of all databases on an instance and mails a monthly report. I am currently working with SQLServer2014. I am using an SQL query that calculates the size of all databases by querying sys.master_files.
The problem is that when using JDBC to make the query, it returns the error:
java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost"
I have tried connecting to particular databases and that works fine. Is there any way to do this query directly to sys.master_files using JDBC? Or is there a smarter way altogether to accomplish the same result?
Thanks
Your "No suitable driver found" error is simply due to a malformed connection URL. jdbc:microsoft:sqlserver is not valid.
As for connecting to an instance without specifying a particular database, this works fine for me:
// NB: no databaseName specified in the following
String connectionUrl = "jdbc:sqlserver://localhost;instanceName=SQLEXPRESS;integratedSecurity=true";
try (Connection conn = DriverManager.getConnection(connectionUrl)) {
String sql = "SELECT name FROM sys.master_files WHERE type_desc='ROWS' ORDER BY database_id";
try (
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql)) {
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
Note that sys.master_files is a system view that is available in all databases, so AFAIK it doesn't matter what the current database (catalog) is when you call it.
I'm developing a web application with Play 2.1.0 and programming it with Java and I need to have access to data already saved in a DB to modify them.
I tried to create a new instance without the new operator and reference it to my object saved in the database, but even if there is no pointer error, it won't change values of attributes. I couldn't figure out why, so I've decided to enter SQL queries directly.
Same thing, it does not seems to have any mistake, but it won't change anything... I think this comes from a bad link to the database :
Here is my code in application.java :
public static Result modifyQuestionnaire(Long id) throws SQLException {
Statement stmt = null;
Connection con = DB.getConnection();
try {
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String query = "SELECT * FROM WOQ.questionnaire WHERE id=id";
ResultSet uprs = stmt.executeQuery(query);
uprs.updateString("name", "baba");
uprs.updateRow();
} catch(Exception e) {
e.printStackTrace();
} finally {
if (stmt!=null) {
stmt.close();
}
}
return redirect(routes.Application.questionnaire(id));
}
And I also try to enter an UPDATE query directly, still the same..
I've looked everywhere and did not find any solution (except Anorm but it seems to work with Scala language)
Btw, if anyone knows a solution with a second instance that refers to the same object (it seems possible but as I say, there is no error but no actions neither), it's fine for me.
Huh, you showed as that you are trying to create totally new connection, so I supposed, that you don't want to use Ebean, but in case when you are already use it, you can just use its methods for the task:
(copied) There are some options in Ebean's API, so you should check it and choose one:
Update<T> - check in the sample for #NamedUpdates annotation
Ebean.createUpdate(beanType, updStatement)
SqlUpdate - you can just perform raw SQL update, without need for giving the entity type
I'm working on a project in Java, where, I have to make modifications in my SQLite Database.
I connected to it and is working pretty fine, except for this weird error.
s.executeUpdate("INSERT INTO STUDENTS VALUES('S0',11)");
...
//many statements... including queries
...
String c2="INSERT INTO STUDENTS VALUES ('S1', 2)";
s.executeUpdate(c2);
s.executeUpdate("DROP TABLE STUDENTS");
The statements s.executeUpdate("INSERT INTO STUDENTS VALUES('S0',11)"); and s.executeUpdate(c2); run perfectly and insert rows into the database. But when it comes to the statement below, I'm getting the weird Database Locked error.
When I changed the query to another, it also worked pretty fine. The error comes when it reaches the ending statement. More precisely, all the queries written above, i.e., the first statement of the code here work pretty fine.
Please help me to find the bug.
I guess that the "s" variable is a Statement. Try closing the resources after you execute:
PreparedStatement updateStatement = connection.prepareStatement("INSERT INTO STUDENTS VALUES ('S1', 2)");
try {
updateStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
updateStatement.close();
}
Do this after every call to the database.
I have a problem and need some enlightenment here..
I am using trigger to detect change made to my database, means that I set all my table with trigger for insert, update, and delete (MySQL)
Then I write that change into a table that I have made specifically to contain all information about the change. Let's name it xtable. (This table is not equipped with trigger)
My Java program need to continuously read that xtable to let other application know about the change.
Well the problem is, when I read the xtable in a loop, I can only read the initial value of the xtable that is when I established the connection to the database. (connection is established outside the loop)
If a change has been made to the database which will lead to new row in xtable, this new row which is produced by the trigger is not detected no matter how many times I read it with executing "select * from xtable" query..
The code look like this:
public static void main(String[] args) {
Connection conn = null;
try {
conn = database.getConnection();
Statement state = conn.createStatement();
String query = "select * from `xtable`;";
while (true) {
ResultSet rs = state.executeQuery(query);
while(rs.next){
// Some code for letting the other application know of the change
}
}
} catch (SQLException ex) {
} finally {
if (conn != null) {
conn.close();
}
}
}
So basically if I run the program while the xtable is empty, I always gain an empty ResultSet even when there is a new row after sometimes.
Actually this problem can be solved by established the connection inside the loop, but then it will lead to another problem because it will consume more and more resource as the loop go around. (I have already try this and it will eventually use all resource on my computer after sometimes even when I have already properly closed it)
So can anyone please give me some suggestion what to do?
This is my first time posting a question here, I am sorry if there is some rule that I don't follow and please give me the right direction.
Thereis such thing as transaction isolation. It could be possible that your connection does not see changes because you did not commited transaction coming from trigger, or you did not started new one on client side. Impossible to tell without seeing your database set up.
PS: Message queuing is way better alternative
I think you'd better consider trigger instead of querying to the DBMS by looping.
If you use trigger you don't have to use that 'while' loop from Java side to check the change of DB.
Instead, trigger mechanism which is embedded in the DBMS will notify the Java side when the change happens.
For Oracle, you can call Java method from PL/SQL.
For PostgreSQL, you can call Java method from PL/Java.
For CUBRID, you can call Java method from Java stored procedure.
For MySQL, you can call Java method but I don't think it is as easy as above.
I wish this link would help you out. http://code.rocksol.it/call-java-from-mysql-trigger
Or google this keyword, "mysql java user defined functions"
Connection connection=getConnection();
statement="query";
try {
stmt = connection.prepareStatement(statement);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null && stmt != null) {
stmt.close();
}
}