I am a newbie and have some question on using jdbc with java:
What changes in the code I will have to make to:
change database type? (i.e. from PostgreSQL to MySQL)
use the table in the code after I decide to drop one of the colums from that table.
Also - how to make queries cached?
change database type? (i.e. from PostgreSQL na MySQL)
Replace the MySQL JDBC driver in the classpath with the PostgreSQL JDBC driver. Update the JDBC connection URL to point to PostgreSQL DB instead of MySQL DB. If necessary, also update your SQL queries to replace any MySQL-specific SQL functions/clauses by PostgreSQL-specific ones.
use the table in the code after I decide to drop one of the colums from that table.
Remove the column in question from the SQL queries. If necessary, also update the entity (the custom Javabean class which you have to represent one row of the DB) to remove the property and getter/setter.
Also - how to make queries cached?
Use PreparedStatement instead of Statement. If possible, replace all the JDBC code by a fullworthy ORM such as JPA or good ol' Hibernate. They not only minimizes JDBC boilerplate code to oneliners, but they also offers second-level caching capabilities as well.
Related
I am looking for an alternative to hibernate.globally_quoted_identifiers for jdbc in order to generate SQL with quotes (to escape reserved keywords) around table names.
We are migrating an oracle database to an h2 in-memory database, and one of the tables is named LIMIT, which is obviously a keyword and bad practice but unfortunately we do no have control over the database structure and it is not something that can be changed.
I found this page that suggests that is can be done on a transactional level, but we need it to be applied globally.
I'm trying to access data in multiple databases on a single running instance. the table structures of these databases are all the same; As far as I know, create a new connnection using jdbc is very expensive. But the connection string of jdbc require format like this
jdbc:mysql://hostname/ databaseName, which needs to specify a specific database.
So I'm wondering is there any way to query data in multiple databases using one connection?
The MySQL documentation is badly written on this topic.
The SELECT Syntax page refers to the JOIN Syntax page for how a table name can be written, even if you don't use JOIN clauses. The JOIN Syntax page simply says tbl_name, without further defining what that is. There is even a comment at the bottom calling this out:
This page needs to make it explicit that a table reference can be of the form schema_name.tbl_name, and that joins between databases are therefore posible.
The Schema Object Names page says nothing about qualifying names, but does have a sub-page called Identifier Qualifiers, which says that a table column can be referred to using the syntax db_name.tbl_name.col_name. The page says nothing about the ability to refer to tables using db_name.tbl_name.
But, if you can refer to a column using db_name.tbl_name.col_name, it would only make sense if you can also refer to a table using db_name.tbl_name, which means that you can access all your databases using a single Connection, if you're ok with having to qualify the table names in the SQL statements.
As mentioned by #MarkRotteveel in a comment, you can also switch database using the Connection.setCatalog(String catalog) method.
This is documented in the MySQL Connector/J 5.1 Developer Guide:
Initial Database for Connection
If the database is not specified, the connection is made with no default database. In this case, either call the setCatalog() method on the Connection instance, or fully specify table names using the database name (that is, SELECT dbname.tablename.colname FROM dbname.tablename...) in your SQL. Opening a connection without specifying the database to use is generally only useful when building tools that work with multiple databases, such as GUI database managers.
Note: Always use the Connection.setCatalog() method to specify the desired database in JDBC applications, rather than the USE database statement.
I'm hitting a problem when trying to update a ResultSet.
I'm querying the database via JDBC, and getting back a resultset which is not CONCUR_UPDATABLE.
I need to replace the '_' into ' ' at the specified columns. How could I do that?
String value = derivedResult.getString(column).replace("_", " ");
derivedResult.updateString(column, value);
derivedResult.updateRow();
This works fine on Updatable, but what if it's ResultSet.CONCUR_READ_ONLY?
EDIT:
This will be a JDBC driver, which calls another JDBC Drivers, my problem is i need to replace the content of the ResultSets, even if it's forward only, or Read only. If I set scroll_insensitive and updatable, there isn't a problem, but there are JDBC drivers that works with forward only resultsets.
Solutions:
Should I try to move the results to an inmemory database and replace the contents there.
Should I implement the resultset which acts like all my other classes: Calls the underlying drivers function with modifications if needed.
I don't want to use the resulst afterward to make updates or inserts. Basically this will be done on select queries.
In my experience updating the result set is only possible for simple queries (select statements on a single table). However, depending on the database, this may change. I would first consult the database documentation.
Even if you create your own resultset which would be updatable, why do you think that the database data would change? It is highly probable (almost certain) that the update mechanism uses code that is not public and only exists in the resultset instance implementation type of the jdbc driver you use.
I hope the above makes sense.
how to fetch data from sybase and load into MS-SQL with java?
Our client have data in Sybase database now I want to take that data and transfer to mssql dataBase with java. Can any body please help me?
Thanks
If you're all set on doing this in Java, it should be easy enough to get JDBC drivers for each Sybase and MsSQL and write the translation logic yourself. That is to say, table by table you are going to need to do...
1. Using JDBC SELECT * FROM table. This will produce a ResultSet.
2. for each member of the ResultSet you will need to do a corresponding INSERT into the MS-SQL database
The trick is going to be that the schema will already need to be set on the MS-SQL side as I know of no way to infer DDL across the JDBC connection. You will also have sequencing issues for any Foreign Key constraints that are in place. Autogenerated values is also going to be a pain-in-the-butt.
There has got to be a way to do this with raw SQL way to do this.
In my database code I use some Hibernate native SQL queries (inserts, deletion, updates). I understand that when I use HQL and the cache is on than the state of the DB is stable whenever I call the DB with the HQL. However, I wonder what happens if I use native SQL queries, e.g. I insert some data (but do not commit it) and than I try to fetch some data with a HQL query. Will I get the inserted data too?
Any hints?
I would say, that it depends on underlying database and transactions setting. But even HQL is translated to native SQL and executed. As long as you stay in same transaction, you will be able to load changes made with native SQL via HQL. But keep in mind, that HQL queries interat with caches, proxies and other hibernate stuff - there could be some weird issues, because natiev SQL completely bypasses this ( this is purpose of native queries - fast lane around all the hibernate stuff )
Adding to Konstantin Pribluda's answer I can say that in the reverse situation : adding data through Hibernate(even with session.save()) and then fetching data with native SQL resulted in the native SQL-query not fetching the added data.
So when using different types of query's in the DAO, I always flush the session first before I use a native SQL query... Never know which method's get mixed in the Service layer.
Yes, you will get it because all of your DAO queries will executed one by one whether its HQL or SQL. So if insert query is first in order then you will get inserted records.