Execute Java upon change in a mysql database [duplicate] - java

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

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

Automate build of the script to update DB in postgres

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

Create mssql trigger from java program

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

JDBC and MySQL: Do changes in the underlying database are known to the java program?

Suppose that, after I establish a connection of my java application with my underlying MySQL database, and then open MySQL workbench and update some rows in a table, and then retrieve the fields of that table in the java client, will those changes synchronize with the java program. In other words, will those changes appear in the results of queries I carry out in the java program?
In general, after establishing a connection between a java application and a MySQL database, are the updates, deletions, alterations etc. carried out in the database by using the MySQL DBMS (and not the java program) visible to the program?
In general, yes.
If they are not showing up, my first guess would be that you are not commiting the updates in the workbench
Only if you run your query again.
Specifically, if you run something like this:
ResultSet resultSet = myStatement.executeQuery("select * from some_table");
and then you update the database (either from the command line / database client), then no. you will need to run your query again to see the changes you made.

Java run by MySQL trigger

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.

Categories

Resources