I am new to java and I am working on two table one table is on server side and another one is on client end. What i want is when there is any update in server database it should reflect in client database and when client data not found in server it should get deleted. In below code i fetch both table data.
Server Table 1
ID | NAME
1 | ABC
2 | ABC
4 | ABC
5 | ABC
Local Table 2
ID | NAME
1 | ABC
2 | ABC
3 | ABC
4 | ABC
Expected Result on Local Table
ID | NAME
1 | ABC
2 | ABC
4 | ABC
5 | ABC
Here is my code
// LOCAL DATA
ResultSet local_data = stmth2.executeQuery("SELECT * FROM emoticon");
local_data.next();
//SERVER DATA
Class.forName("com.mysql.jdbc.Driver");
Connection con9 = DriverManager.getConnection("jdbc:mysql://localhost:3306/mood","root","");
Statement stmt9 = con9.createStatement();
ResultSet server_data = stmt9.executeQuery("SELECT * FROM emo");
Is there any standard way to achieve this
If both tables are on different MySQL servers, you might use replication.
Otherwise, if the local table is not in another MySQL server, you'll maybe find a way to mimic a MySQL server in your Java program. That is, make it useable as a slave for replication anyway.
Or, this should always be possible in your Java program, use polling. Have a thread checking the remote table and synchronizing it with the local one every n secs. Of course that's not too gentle on resources though. And there will be a delay until changes are propagated to the client.
Related
I am using Microsoft SQL Server with already stored data.
In one of my tables I can find data like:
+--------+------------+
| id | value |
+--------+------------+
| 1 | 12-34 |
| 2 | 5678 |
| 3 | 1-23-4 |
+--------+------------+
I realized that the VALUE column was not properly formatted when inserted.
What I am trying to achieve is to get id by given value:
SELECT d.id FROM data d WHERE d.value = '1234';
Is there any way to format data in column just before SELECT clause?
Should I create new view and modify column in that view or maybe use complicated REGEX to get only digits (with LIKE comparator)?
P.S. I manage database in Jakarta EE project using Hibernate.
P.S.2. I am not able to modify stored data.
One method is to use replace() before the comparison:
WHERE REPLACE(d.value, '-', '') = '1234'
I am developing an app in which the user first login,and if he doesn't have an account he will register after finishing that step he will be directed to a page,there he will add users to his profile like.. his own family members anything like that and when they add a user its going to be saved in a table on an external server(SQL) and then get that in the user list view but how can the user only see his own users and nobody else can see his users except him and so for all other users,like their own data?!
i didn't figure out how to do this yet,any one help me with this task?
any help would be appreciated
It feels like your question is about how to design your database rather than the technical details of how to call the database from you code and display results on your pages?
You need to split your data up into multiple tables. Each table should have a unique id (the id for each row should be different). Then you link rows in tables together by referencing the ids.
One way to achieve what you want would be like this.
A user table:
User
----
Id
Username
Password (hashed)
A family members table:
Family_Members
--------------
User
Member
You put pairs of relationships in the family_members table. So you would put the id of one user in the first column user and the id of one of their family members in the second column member. Then you have a row for every relationship. When you load the table, you find all the rows where user is equal to the user id you are loading.
For example:
User
+--+--------+--------+
|Id|Username|Password|
| 1|Bob |abc |
| 2|Dad |def |
| 3|Mum |ghi |
+--+--------+--------+
And..
Family_members
+----+------+
|User|Member|
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 3 |
| 3 | 1 |
| 3 | 2 |
+----+------+
Then when you load family members for Bob, who's id is 1, you load all the rows from the family_members table that have User==1. Then you go back to the User table and load the details you want for each family member, and get an object like:
User:
- username: Bob
- password: abc
- family:
- User:
- username: Dad
- User:
- username: Mum
Hope this answers your question.
I just need this last piece of the puzzle to finish my plugin. Currently I am having a problem with how to set up my MySQL table for all alt accounts that are logged into a server. I know that I either need to have a set number that is super high and make the uuid's add to the next empty cell or just add a new column for every uuid, but I just need to know the most efficient way to add all UUID's to a single IP (The primary key). Looks something I like at the moment:
IP |
Row #1
Row #2
etc
Don't use the IP as Primary Key. The fact that a Primary Key is unique and you have multiple occurrences of IP addresses with different UUIDs makes it hard to accomplish what you need.
Try something like this:
id (PK)| ip_address | uuid | date
--------------------------------------
1 | 1.2.3.4 | as-df-gh | 12345
2 | 1.2.3.4 | df-as-gh | 12346
3 | 2.3.4.5 | as-gh-df | 12347
4 | 3.4.5.6 | as-df-gh | 12348
Whenever someone logs in you can then add another row (or if you don't need the login date column, first check if there's already one with the IP / UUID pair and skip it).
Now you can select all UUIDs from a certain IP address:
SELECT uuid FROM your_table WHERE ip_address = '1.2.3.4'
results in
uuid
--------
as-df-gh
df-as-gh
Or the other way around:
SELECT ip_address FROM your_table WHERE uuid = 'as-df-gh'
results in
ip_address
----------
1.2.3.4
3.4.5.6
I have an SQLite table like:
+---+-------------+----------------+
|_id| lap_time_ms |formatted_elapse|
+---+-------------+----------------+
| 1 | 5600 | 00:05.6 |
| 2 | 4612 | 00:04.6 |
| 3 | 4123 | 00:04.1 |
| 4 | 15033 | 00:15.0 |
| 5 | 4523 | 00:04.5 |
| 6 | 6246 | 00:06.2 |
Where lap_time_ms is an of the type long and represents the amount of time in milliseconds for a lap while formatter_elapse is a String that represents the formatted (displayable) form of the first column (elapse).
My question is that if I want to add (say) 5 seconds (5000) to each lap_time_ms then I use a statement like:
DB.execSQL("update table_name set KEY_ELAPSE=KEY_ELAPSE+5000);
Which works fine however the problem is that formatted_elapse still retains its outdated value!
So, what is the best way to update the values in the formatted_elapse column if I have a function like:
public static String getFormattedTime(long milliseconds) {
// custom code that processes the long
return processedString;
}
It may be a long shot (metaphorically speaking of course ;) but is it possible to have SQLite link the two columns such that if I update a lap_time_ms row, the formatted_elapse will automatically update appropriately.
Thanks in advance!
In theory, it would be possible to create a trigger to update that column, but only if the formatting can be done with some built-in SQLite function (Android does not allow user-defined functions):
CREATE TRIGGER update_formatted_elapse
AFTER UPDATE OF lap_time_ms ON MyTable
FOR EACH ROW
BEGIN
UPDATE MyTable
SET formatted_elapse = strftime('%M:%f', NEW.lap_time_ms, 'unixepoch')
WHERE _id = NEW._id;
END;
However, it would be bad design to store the formatted string in the database; it would be duplicated information that is in danger of becoming inconsistent.
Drop the formatted_elapse column and just call getFormattedTime in your code whenever you need it.
Is it possible index a complete database without mentioning the table names explicitly in the data-config.xml as new tables are added everyday and I cannot change the data-config.xml everyday to add new tables.
Haven table names based on the date smells like there is something wrong in your Design. But given this requirement in your question you can add Data to your solr server without telling you have a DB. You just have to make sure you hav a unique ID for the data record in you solr Server with whom you can identify the corresponding record in your DB, something like abcd_2011_03_19.uniqueid. You can post the data to solr in Java in solrj or just plain xml or json.
Example:
--------------
| User Input |
--------------
|post
V
-----------------------------------
| My Backend (generate unique id) |
-----------------------------------
|post(sql) |post (e.g. solrj)
V V
------ --------
| DB | | solr |
------ --------
My ascii skillz are mad :D