SQL Server To MY SQL - java

I got a new project from my teacher to convert database to another. How can I convert a MS SQL database into MySQL using Java?

You will want to keep in mind that there are two logical steps regardless of the Programming environment. Firstly, you will want to map the schema of the database to an equivalent schema in the target database. This means mapping data types and constraints. Sometimes there are cases where it is simply not possible. Secondly, mapping the data from one database to the other. Timestamps and date formats must be equivalent for example. Hope that helps you to get started.

How can I convert a MS SQL database into MYSQL using JAVA
Dump it ( from MSSQL) and execute the result in Mysql.
With a question like that it's the best answer you can give

You could also try to use the hibernate tools to create a mapping to the MS SQL tables. Then use hibernate again with the created mapping to create the tables in MySQL.

Look at the various metadata classes in JDBC:
java.sql.DatabaseMetaData
java.sql.ResultSetMetaData
These will allow you to get the structure from the MSSQL DB. You need to do a little experimentation as, if memory serves, what gets returned in the metadata can vary from database to database.
Once you've worked out how to get the metadata from the MSSQL DB you need to convert that into SQL commands to recreate the structure in the MySQL DB.
Then with the structure in place you can start copying the data between the two by creating insert queries that run against the MySQL DB from querying the contents of the MSSQL DB. You'll need to do this in the correct order so that the referential integrity isn't violated which again you'll be able to determine from the metadata.
It's an interesting academic exercise but in the real-world you'd use an Extract-Transform-Load (ETL) tool.

Related

How to use COPY table command of Oracle DBMS using JDBC?

I'm trying copy table from one database to another(On different machines), and using JDBC Template to execute query, but this request is specific to Oracle:
COPY FROM username1/passwd1#//192.168.3.17:1521/PROD_SERVICE to username2/passwd2#//192.168.4.17:1521/SANDBOX_SERVICE INSERT TABLE_C (*) USING (SELECT * FROM TABLE_C);
And I get error:
Caused by: java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement
How can I use specific to Oracle syntax in JDBC?
Like some of the comments have already clarified, COPY is a sqlplus command, and it has been deprecated for a while. You cannot use it inside JAVA, because this command is not part of the SQL engine, it's just a kind of additional feature available only in sqlplus. It is still available, but only for backwards compatibility.
If you want to copy a table using Java, you need to understand first some things:
Java, or any external engine for that matter, can't connect at the same time to both databases. Either it connects to one or to the other.
You need to have a kind of bridge between both databases, so that your Java program is only acting as trigger.
Copying tables between databases is something related to the database, so you should think in using tools provided by your database engine. You have some options, like Datapump or RMAN, although I consider Datapump the best suitable for your scenario.
However, if you insist in using Java, first you need to have a database link between both databases. Then you can use Java to invoke an insert from one database to another.
https://docs.oracle.com/database/121/SQLRF/statements_5006.htm#SQLRF01205
If you don't want to depend on thsnames entries in the server, here an example of database links:
CREATE DATABASE LINK to_my_remote_user
CONNECT TO remote_user IDENTIFIED BY password
USING '(DESCRIPTION=
(ADDRESS=(PROTOCOL=TCP)(HOST=remote_server)(PORT=remote_port))
(CONNECT_DATA=(SERVICE_NAME=remote_service_name))
)';
Once you have the dblink created, then you can connect from java to the database where the link is available and copy the data to the remote database
INSERT INTO remote_user.remote_table#to_my_remote_user
select * from local_user.local_table ;
Important: Normally dblinks are not allowed on Production systems, because they increase security risks. Also remember that DDL operations over a database link require an extra step, such as using the procedure DBMS_UTILITY.EXEC_DDL_STATEMENT#dblink('create table ...);
Another option outside of Java is using SQL Developer copy feature. Although I only recommend it for small tables. If you want to use it with big tables, it will probably hang. You can read here an good example :
copy from one database to another using oracle sql developer - connection failed

How to convert a mwb schema in Java entities for Hibernate

How can I convert a schema that I have in Sql Workbench to Java entities for Hibernate?
Is there an easy way?
I would first generate the SQL script from MySQL WorkBench, then physically create the schema in a local database instance.
You can then generate the Hibernate entities direct from the database.
Plenty of guides online for this last step. E.g. http://www.mkyong.com/hibernate/how-to-generate-code-with-hibernate-tools/

Can I use JPQL to query a MySQL database which was populated by loading CSV files?

This is something of a noob question, so please bear with me.
I'm building a Java web app which is deployed on JBoss. Part of the functionality is populating a MySQL DB with data from an Excel spreadsheet. This can be achieved in 2 ways:
Using JExcel / Apache POI to parse the spreadsheet data and creating Entity "beans" which are then persisted to the DB.
Using scripts to convert the spreadsheet to csv files and then load the csv files into the DB.
My question is: If I choose the scripting / csv route, can I still use JPQL to query the DB or will I have to resort to native SQL queries in the Java code?
JPQL can be used to query table independently from method that was used to populate table. Data stored to table is not aware of with which method it was inserted.
JPA is not notified about changes made to data via script, but in typical use case with no additional caches and transaction-scoped PersistenceContext that is not the issue, because query will hit the database and deliver fresh data.

Any WorkAround For Java ResultSet Limitation

I am doing a database migration work. I have to copy a database in MSSQL to MySql database. It was possible to come up with a small java utility to copy table stucture from MSSQL to MySql Database. Now i have to copy all data from MSSQL to MySql. I tried using resultset in java to obtain all data from a table but then it could only fetch a small part of data. Is there any alternate solution to get all data from table to resultset or to some other similar structure which i could possibly use, to insert the same data into mysql Db. There are more than 25,00,000 records for a table.
A JDBC result set should in principle allow you to iterate the entirity of a large query result.
However going via Java may not be the most efficient approach. Bulk export to a file and bulk import may be the way to go. It appears that MS has a bcp utility that may do the export.
The best way to achieve a database migration like you describe is to use and ETL Tool - there's a good overview of ETL here:
http://en.wikipedia.org/wiki/Extract,_transform,_load
There's no reason why you wouldn't be able to do this with JDBC and so if you are set on rolling your own please elaborate on 'could only fetch a small part of data':
what is the query you are running?
are you getting an exception?
which JDBC driver are you using to connect to MS-SQL?

Bulk Row Transfer between Oracle Databases with a Select Filter

Basically, I'm trying to selectively copy a table from one database to another. I have two different [Oracle] databases (e.g., running on different hosts) with the same schema. I'm interested in a efficient way to load Table A in DB1 with the result of running a select on Table A in DB2. I'm using JDBC, if that's relevant.
Use a database link, and use create table as select.
create database link other_db connect to remote_user identified by remote_passwd using remote_tnsname;
create table a as select * from a#other_db;
If the databases are from the same vendor they usually provide a native way to make a view
of a table in another database. in which case, a "select into" query will do it no problem
Oracle, for example, has the database link which works pretty well.
Outside of that you are going to have to make a connection to each database and read in
from one connection and write out to the other.
There are tools like Oracle's ODI that can do the legwork, but they all use the same
read in, write out model
You may not even need to move that data. Maybe you can just select across the database link.

Categories

Resources