Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to listen for changes in an MySQL database table using Java and JDBC?

I have a number of users which are logged in at a time in my desktop application. They are working on the same table (create, read, update, delete data) so I have to update their views, to reflect changes, every few seconds - currently I am thinking to use a different thread to do that.

I am using the MySQL database engine.

Is there a way, using JDBC, to listen for changes on a specific table in the database and triggering a Java method only when changes are made?

like image 657
adrian7 Avatar asked Apr 21 '10 12:04

adrian7


People also ask

Can Java connect to MySQL database?

In Java, we can connect to our database(MySQL) with JDBC(Java Database Connectivity) through the Java code. JDBC is one of the standard APIs for database connectivity, using it we can easily run our query, statement, and also fetch data from the database.

How do you query a database in Java?

STEP 1: Allocate a Connection object, for connecting to the database server. STEP 2: Allocate a Statement object, under the Connection created earlier, for holding a SQL command. STEP 3: Write a SQL query and execute the query, via the Statement and Connection created. STEP 4: Process the query result.

What is JDBC connection with example?

getConnection() method to create a Connection object, which represents a physical connection with the database. Execute a query − Requires using an object of type Statement for building and submitting an SQL statement to the database. Extract data from result set − Requires that you use the appropriate ResultSet.


1 Answers

No, mysql does not support sending async notifications to a client, nor does it have any ways of waiting for table changes.

You'd have to build your own data access layer, where you support this in your code - and all data access would have to go through that same code.

like image 156
nos Avatar answered Sep 30 '22 23:09

nos