I am trying to create some MySQL code that will invoke a Java program from a trigger.
Here is what I have so far:
CREATE TRIGGER trig_name after insert ON studentinfo
FOR EACH ROW
BEGIN
END
The trigger content would then call the Java program. Is this possible?
Though not a standard feature this is very well possible with MySQL. You can use the SELECT .. INTO OUTFILE statement from inside the trigger to write to a named pipe (Windows) or memroy filesystem (Linux). Both of those can easily be monitored from Java code (or any other code for that matter). Using this technique you will avoid polling and because no actual disk access takes place either you will have good performance.
I have written a Java package for this actually so I'm 100% sure it is possible and performs well. Unfortunately I am not allowed to share my efforts here (my previous answer was deleted by a moderator) so you will have to code it yourself, sorry.
A direct answer: no you can't call a java method from a mysql trigger. If you had an oracle database you could, but not mysql.
To do what you want to do with mysql you can
make the code that updates the database also notify the swing application. Or you can
make the trigger accumulate data on pending operations in a separate table that you read periodically from the swing app.
Calling a java method from an SQL database isn't a standard feature. The Informix DB can call a shell script from a stored procedure, but I don't know of a feature like this in MySQL (I'm not an expert on mysql).
The closest thing that works with all databases would be to have a thread and periodically poll the database for new records.
SELECT * FROM studentinfo WHERE id > last_seen_id
Or you could use a timestamp:
SELECT * FROM studentinfo WHERE create_date >= last_seen_create_date
In this case you would have to filter duplicated rows which have already loaded from the previous run.
Related
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
I am trying to create some MySQL code that will invoke a Java program from a trigger.
Here is what I have so far:
CREATE TRIGGER trig_name after insert ON studentinfo
FOR EACH ROW
BEGIN
END
The trigger content would then call the Java program. Is this possible?
Though not a standard feature this is very well possible with MySQL. You can use the SELECT .. INTO OUTFILE statement from inside the trigger to write to a named pipe (Windows) or memroy filesystem (Linux). Both of those can easily be monitored from Java code (or any other code for that matter). Using this technique you will avoid polling and because no actual disk access takes place either you will have good performance.
I have written a Java package for this actually so I'm 100% sure it is possible and performs well. Unfortunately I am not allowed to share my efforts here (my previous answer was deleted by a moderator) so you will have to code it yourself, sorry.
A direct answer: no you can't call a java method from a mysql trigger. If you had an oracle database you could, but not mysql.
To do what you want to do with mysql you can
make the code that updates the database also notify the swing application. Or you can
make the trigger accumulate data on pending operations in a separate table that you read periodically from the swing app.
Calling a java method from an SQL database isn't a standard feature. The Informix DB can call a shell script from a stored procedure, but I don't know of a feature like this in MySQL (I'm not an expert on mysql).
The closest thing that works with all databases would be to have a thread and periodically poll the database for new records.
SELECT * FROM studentinfo WHERE id > last_seen_id
Or you could use a timestamp:
SELECT * FROM studentinfo WHERE create_date >= last_seen_create_date
In this case you would have to filter duplicated rows which have already loaded from the previous run.
We have a system made in java using a postgres database.
This database changes often, and once a week or less we are updating it. These changes are in the struture of the DB (DDL), usually in functions and fields to add new functionality.
For the changes in the DB we usually use navicat as follows:
1- We made the change in the structure of the DB using navicat and we copy the SQL that gives us to an XML file for each change we made.
2- When we have to update the DB in production we check files, identified by a version number, and update the DB.
3- Then we repeat this for each DB installed (30 in total)
The problem that we are having is that as the whole process is manual and is very easy to forget to copy a change to the XML so when we use it the script does not work or even worse when the system needs this change fails.
Therefore we are looking for a way to automate this task and we came with the following idea:
1- We make changes in navicat
2- Configure the postgres to LOG the changes in the DDL into a CSV file
3- Later we read the CSV file and pass the changes to the XML to update the producction DB
The problem we are having is that the LOG will save all attempts to change the structure, including errors so if we use that script to update it will fail too.
Is there some way to save only successful DDL changes in the log in postgres?
Is there a script or application to get the DDL changes and put it in script automatically?
Is there a better way to automate this process?
there are many answers for the questions above :-) i have managed rapidly changing databases using a number of schemes. one way to do it is maintain a master database (like you have). Use dbtoyaml to create a yaml description of the database. Then use yamltodb on all of the (30) targets, which will do everything necessary to make the target databases look exactly like the master. I have used this software for about 6 months, it is fantastic. pyrseas. -g
Is it possible to create a trigger from java using mssql jdbc driver?
I am having one audit trigger which is saved in a .sql file. The UI will show all the tables available in client db. Sometimes they need to enable the auditing for some specific tables, so I need to run the sql script by replacing the table name with the user selected one. The client needs this to be achieved from our java software. As I am new to JDBC, I am unable to figure this out. Any better ideas/help appreciated
Execute trigger creation SQL as a plain query. You may also need to wrap the SQL with exec('...') as mentioned in: https://stackoverflow.com/a/951956/92063
I need to copy all the data from MS SQL server to MySQL server. I am planning to use the Quartz scheduler to perform this. This scheduler will run every night and move the data from MS-SQL-Server to MySQL server. Can anyone please tell if this is fine or is there any other better way to do this?
Update:
I need to transfer only one table with 40 columns (from MS SQL server to MySQL)
I wouldn't involve java unless I absolutely had to: java would be adding no value but would be adding extra complexity.
This is a "DBA" type task that belongs in a script scheduled with cron tab.
If I was implementing it, I would export the source database as an SQL script then import it by running it on the target.
SQL Server Management Studio's "Import Data" task (right-click on the DB name, then tasks) will do most of this for you. Run it from the database you want to copy the data into.
If the tables don't exist it will create them for you, but you'll probably have to recreate any indexes and such. If the tables do exist, it will append the new data by default but you can adjust that (edit mappings) so it will delete all existing data.
I use this all the time and it works fairly well.
by- david
I would recommend you to use http://www.talend.com for tasks like that.
UPDATE
Talend Open Studio for Data Integration is Opensource, there are some other features which are propietary details here
As PbxMan said, I would use an ETL, but I recommed Pentaho (http://wiki.pentaho.com/display/EAI/Spoon+User+Guide) which I think is far easier for such simple jobs
I agree with #bohemian - running a job to transfer a single table every night sounds like a great candidate for a cron job (or "scheduled task" if on Windows). Using a framework like Quartz for this task seems like overkill.
There are many solutions for moving data from SQL Server to MySQL, others have listed great solutions such as Talend. If it would benefit you to only transfer certain columns (for example, to avoid leaking PII), my product SQLpipe might help, as it transfers the result of a query, not an entire table.
Here is a blog post showing how to use the product to transfer data from SQL Server to MySQL.