#Detect if a mysql database column in jdbc has changed
1 messages Β· Page 1 of 1 (latest)
<@&987246584574140416> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
depends on ur database
strings itself, as far as java is concerned, cannot change
strings are immutable
so ur looking at some database specific way to get notified when a column changed
not when a java-string changed
so u have to give us more details about what DB ur using and what DB framework ur using and how its setup in code
for example, are u using jpa? hibernate? just jdbc? ...
and which db is it?
mysql
no worries π
i dont think so. im currently checking if mysql has sth for that, but i doubt
are the changes done by ur java app or also from elsewhere?
ye
if its former, u could emit such events urself within java
if latter, ur probably out of luck
(other than checking every couple of seconds urself)
and is using for example like a while(true) loop a bad idea?
basically, i have a minecraft plugin that puts all of the online player's names in the database and i want to display all the online players in a dc channel (with a dc bot)
bad idea, yes
and i need to delete the "<player> is online" message every time the player leaves and send a message when they join
can u please answer whether the DB is only changed by ur java code or also by someone else
only from my java code
then just go to where ur doing the string change and send out the event there
if ur in charge of when the change is happening, u can just notify/trigger the part of ur code that needs to be triggered
no, actually not
.
okay. sure u need to act on a DB change? u just said its when a player joins/leaves etc. minecraft gives u events for that already, no?
ye it does
that code is already finished
now u just have to scan of theres a change in the column
ye
yes to which?
it only changes when a player leaves/joins
then just do the check there
when a user joins/leaves, read the column and act if its different to before
but again, the mc plugin and the discord bot are 2 different programms
so the only information i have from the plugin is the database itself
Detect if a column in mysql in ldbc has changed
Detect if a column in mysql in jdbc has changed
ah. u have two apps and they are supposed to work with each other?
Detect if a mysql database column in jdbc has changed
yea, exactly
well, i would probably setup some sort of event system so that ur plugin can send events to the discord bot
but thats probably a major redesign
so let me instead give u sth quick and dirty that works well for today
alr
u previously mentioned "while (true) check db ..."
thats called active-polling and its generally not good bc
- u put a lot of stress on the system, even though 99% of the time it wont find a change
- ur missing the actual moment it changed by a few millis if ur check-rate isnt small enough
however, this general idea does work
and its sth u can do
the proper way to do this in java is via a scheduled executor service
looks sth like
i mean, i dont really need to know when exactly a player joined/left the server, it can be delayed by a few milliseconds or maybe like 1-2 seconds
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
...
servive.scheduleAtFixedRate(this::checkDB, 0, 200, TimeUnit.MILLISECONDS);
...
private void checkDB() {
... // read db, do ur thing
}
with that, ur reading the db 5 times per second, which it will laugh at. so thats no problem at all. stress-wise
and ur reaction will be at max 200ms delayed
which is still quite fast
via active-polling
which is always the last option to consider, cause its the worst
better would be if u can get ur plugin to notify u instead
i.e. react on-event
instead of checking urself
so but there isnt an event for a db change in jdbc?
nope
some DBs might offer sth for that, but i couldnt find anything for mysql with a quick search
like, i know that u can setup hooks to be executed within some DBs on column changes. but no idea how to connect that to ur java via jdbc
its scary that ur plugin writes to a DB directly anyways
wdym?
there are more concerning issues in ur design than ur current issue
wait, is the mc plugin a server or client plugin?
is the plugin executed on the same machine than the discord bot?
and on the same machine that has the DB?
no, both i host externally
lmao chatcringe gave me this answer
big oof
ye
is it a server or client plugin?
server plugin (spigot)
and the server runs on a different machine than the discord bot?
ye
but both have access to the DB?
no, both have access to the db
okay. so if done "properly", ur discord bot would offer some rest-api endpoint for the plugin to call
that way the plugin could notify the discord bot about events
and for example forward all kinds of events to it
and the bot could then act on it
i made the db assessable from anywhere (for testing)
on the long run, this would be the proper approach
and it would also help u for other situations
cause u could then tell the discord bot every single event
nah, but actually its fine of theres a small delay
but i totally get that this is a bit overkill for today
another funny approach would be to abuse discord webhooks
because sending a message is also like random....
this is actually quite clever
i.e. u could setup a hidden channel in discord
enable webhooks on it
and then let the plugin send messages to that webhook
thats quite simple
ohhhhh
and in ur discord bot u can then react on messages received in this channel
and by that act
so ur plugin could write to that hidden discord channel "player x joined"
ur discord bot reads it with onMessageReceived
and by that do sth
i actually never quite really understood what dc webhooks are
well, its just an url and u send ur text message to it
and discord will then send it as message in that channel
its super simple
that way ur plugin can freely post messages in discord
which ur discord bot can read
so i wouldnt need a db??
make an audit table
with login/logout events and time
thats one way to go about it
wdym audit table?
so how do u send a message to a channel via a webhook?
well, DB is still the best way to persist
but u can use the discord webhook to transport messages to ur discord bot
for example events
its a very simple http post request
4 lines in java with HttpClient
there are also libraries, then its even easier
isnt there a built in method in jda?
i think so. let me check our code base for tj-bot
cause we also use webhooks it for #tjbot-log-info
seems its not in jda
but
implementation 'club.minnced:discord-webhooks:0.8.2'
then u just do
WebhookClient webhookClient = WebhookClient.withUrl(url);
and
webhookClient.send(...);
with either the text or an embed
so its really simple to use with a lib like that
but u can also do it without libs with javas HttpClient
as its just a http post request
can i also delete a message via webhook?
yes
and also edit
but note that everyone who has the webhook url can do that without any extra security checks
so u have to keep the webhook url secret
ok
whats the maven dep for it?
for that webhookclient class thing
since its not in jda
ye
either u use that or javas HttpClient
no, i wanted to use that
π
there is also a website to easily send messages manually
u can click here and it shows u the full message:
and that u just have to send as a POST request to the url
thats it
but sure, u dont want to write that json urself. so just use the WebhookClient dependency and everything works out of the box
for that webhookclient class thing
since its not in jda
not sure which part of that sentence is unclear to u
yea that webhook thing
yeah. so everything clear now?
i dont understand....
WebhookClient webhookClient = WebhookClient.withUrl(url);
...
webhookClient.send(...);
thats the code, right
ye
if u write that now, it wont compile
ur java will say "who the frick is WebhookClient, never heard of it"
u have to add this external code to ur project first
just like u did with JDA
or any other dependency
the dependency u need for this is club.minnced:discord-webhooks:0.8.2
yes BUT i wanted to know what the dependency of that webhook thing is SO i can use it lmao
it is club.minnced:discord-webhooks:0.8.2
and for maven?
<dependency>
<groupId>club.minnced</groupId>
<artifactId>discord-webhooks</artifactId>
<version>0.8.2</version>
</dependency>
finally, lmao ty
lol π
for the future, just copy paste it into google
first result is the mavenrepository i just shared
and there u can click on maven/gradle/...
alr

fun fact, its made by the same author than jda
iirc they moved it out of jda as seperate project
i suspect there are a lot of people who just want to send some webhooks without getting the full jda
yes π
now u have it in discord
and ur discord bot can pick it up with its "on message received" event
and do whatever it needs to do
that way u can transport any info from plugin to the bot
but dont spam it too much. there are rate limits if u start sending 100x per second lol
id use the DB as much as possible and just notify the bot via discord for certain events
sounds like u didnt add it as dependency properly
i did
but maybe thats also a minecraft quirk, no idea how to add dependencies to mc plugins
Changed the category to Minecraft.
<@&987246652869971988> please have a look, thanks.
(^ paper, add a dependency to a server plugin)
i added that like i would add a dep to any other app
maybe u have to take extra steps for mc plugins. i have no idea. a Minecraft-Helper might know
wdym overdo?
reading the DB 1000x per second
oh alr
what does the ,0 ,200 stand for?
if u mousehover it, u should see the javadoc explaining it.
in a nutshell, the first value is the initial delay and the second the delay for every other invocation
as in "execute every 200 millis, start right now"
doesnt matter where
once u call it, it will execute this method every 200ms forever
(until it crashed or u explicitly cancelled it)
also should i getconnection to the database in the loop?
i meant the looped method
lmao
π
if u have hikari, u can grab a connection from it and it will be cached
if not, u should try to avoid reopening a connection every 200ms
but instead try to keep one open for longer
that said, i doubt u would notice any problems if done wrong
unless ur going crazy and reduce the rate from 200ms
200ms is still slow enough so that mistakes dont really have any impact
would this work?
static ResultSet resultSet;
public static void checkDB() {
try {
Connection connection = getConnection();
Statement statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT player_name FROM player_join_notifications");
} catch (SQLException e) {
throw new RuntimeException(e);
}
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(ShowOnlinePlayers::timer ,0,200, TimeUnit.MILLISECONDS);
}
private static void timer() {
if (resultSet.next()) {
//CHECK THE PLAYERS
}
}
i call checkDB() on startup and getConnection() connects to the db via Drivermanager.getConnection()
u have to do this resultSet = statement.executeQuery("SELECT player_name FROM player_join_notifications"); thing in timer()
u can maybe reuse the connection, but not the rest
oh okay
like this u mean?
static Statement statement;
public static void checkDB() {
try {
Connection connection = getConnection();
Statement statement = connection.createStatement();
} catch (SQLException e) {
throw new RuntimeException(e);
}
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(ShowOnlinePlayers::timer ,0,200, TimeUnit.MILLISECONDS);
}
private static void timer() {
try {
ResultSet resultSet = statement.executeQuery("SELECT player_name FROM player_join_notifications");
if (resultSet.next()) {
//CHECK THE PLAYERS
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}```
Detected code, here are some useful tools:
not sure if u even have to create the statement again each time
been some time i used jdbc
alr ill try