I want to insert data from my Microsoft Access database into a textfield.
I have already made a login system. I only want to insert the data into a textfield from the account that's logged in. With data I mean first name, last name etc.
What's the best way to do this in Java?
You could use this library to read a value from ms access file: http://jackcess.sourceforge.net/ and after put it into textfield.
You need to create a sql connection to the Access database using a JDBC driver for MS Access.
You then need to execute a SQL command against that Access database to retrieve the data you wish to display. This data will be returned as a SQL ResultSet object.
Have you worked with databases before? If not, there is perhaps some reading you need to do on the subject. Check this google search
Well, you read the data from the database using JDBC or some more highlevel object mapper like Hibernate or Eclipselink and map the returned data to your textfields.
Basically you need several steps:
a query to get the data for the logged in account
execute that query on the database using JDBC or something more highlevel
put the returned result into your textfields
If you want to update the data as well, you'd need to
extract the data from your textfields
create an update query and associate the data with it
execute the update query
Some things to take into account:
do you need some lock mechanism to prevent lost updates/dirty reads?
do you want to validate the data before persisting it?
Related
I am trying to log a “change summary” from each INSERT/UPDATE MySQL/SQL Server query that executes in a Java program. For example, let’s say I have the following query:
Connection con = ...
PreparedStatement ps = con.prepareStatement(“INSERT INTO cars (color, brand) VALUES (?, ?)”);
ps.setString(1, “red”);
ps.setString(2, “toyota”);
ps.executeUpdate();
I want to build a “change set“ from this query so I know that one row was inserted into the cars table with the values color=red and brand=toyota.
Ideally, I would like MySQL/SQL Server to tell me this information as that would be the most accurate. I want to avoid using a Java SQL parser because I may have queries with “IF EXISTS BEGIN ELSE END”, in which case I would want to know what was the final query that was inserted/updated.
I only want to track INSERT/UPDATE queries. Is this possible?
What ORM do you use? If you don't use one, now could be the time to start - you give the impression that you have all these prepared statement scattered throughout the code, which is something that needs improving anyway.
Using something like Hibernate means you can just activate its logging and keep the query/parameter data. It might also make you focus your data later a bit more (if it's a bit haphazardly structured right now).
If you're not willing to switch to using an ORM consider creating your own class, perhaps called LoggingPreparedStatement, that is identical to normal PreparedStatement (subclass or wrapper of PreparedStatement such that it uses all the same method names etc so it's a drop in replacement) and logs whatever you want. Use find/replace across the code base to switch to using it.
As an alternative to doing it on the client side, you can get the database to do It. For SQL server it has change tracking, don't know what there is for MySQL but it'll be something proprietary. For something consistent, most DB have triggers that have some mechanism of identifying old and new data and you can stash this in a history table(s) to see what was changed and when. Triggers that keep history have a regularity to their code that means they can be programmatically generated from a list of the table columns and datatypes, so you can query the db for the column names (most db have some virtual tables that tell you info about the real tables) etc and generate your triggers in code and (re)apply them whenever schema changes. The advantage of using triggers is that they really easily identify the data that was changed. The disadvantage is that this is all they can see so if you want your trigger to know more you have to add that info to the table or the session so the trigger can access it - stuff like who ran the query, what the query was. If you're not willing to add useless columns to a table (and indeed, why should you) you can rename all your tables and provide a set of views that select from the new names and are named the old names. These new views can expose extra columns that your client side can update and the views themselves can have INSTEAD OF triggers that update the real tables. Doesn't help for selections though because deleting data doesn't need any data from the client, so the whole thing is a mess. If you were going that wholesale on your DB you'd just switch to using stored procedures for your data modifications and embark on a massive job to change your client side calls. An alternative that is also well leveraged for SQL Server is the CONTEXT_INFO variable, a 128byte variable block of binary data that lives for the life of your connection/session or it's newer upgrade SESSION_CONTEXT, a 256kb set of key value pairs. If you're building something at the client side that logs the user, query and parameter data and you're also building a trigger that logs the data change you could use these variables, programmatically set at the start of each data modification statement, to give your trigger something more involved than "what is the current time" to identify which triggered dataset relates to which query logged. Generating a guid in the client and passing it to the db in some globally readable way that means the database trigger can see it and log it in the history table , tying the client side log of the statement and parameters to the server side set of logged row changes
I want to update a column of a table. But the required Data is available in another oracle DB.Which is the best way to copy data from remote DB to my DB.
Should I go with Java program or can I achieve it in PL/SQL itself?
If I correctly understood the question, you need to set the value of a column with a value extracted from another table of another DB. The two DBs have different structure.
In this case you can do it with just SQL and a database link.
Here's how to create an Oracle database link: Oracle documentation for database links
Then you can write a query like following:
UPDATE local_table
SET local_column = (SELECT remote_column FROM remote_table#remote_db WHERE ...)
WHERE ...
I am a beginner with couch db and ektorp. I want to use ektorp to retrieve one property from all the existing documents in couch db.
Till now I have learnt that couch db gives us the result of the following equivalent SQL query:-
Select * from Customer;
Is there any ektorp way of retrieving the result set of the following SQL query, Apart from the normal Map/reduce solutions of couch db?
Select name from customer;
The result set of the above query could somehow be retrieved in List or Set<> in java.
Any help would be appreciated.
if you only want one field returned from couchdb you must define a view that only contains that field.
however, it is generally best not to create that fine grained views. load the whole doc instead and transform the result in the application layer instead.
I am just trying to make a project in which there might be many employee and doctors may access. it will contain a database and interface and i want to do it in java.But now i am facing a problem -i am not sure how to make the database be able to update at a time ,i mean when an employee update the database the other can access the updated database.i have previously use database in the local host where user is one.
You can use synchronized blocks or monitorfunctions with semaphors in Java.
Put this around the method calling the database and you can control number of simultaneous access to the database.
My app uses a SQLite database for the information. I have a function that checks to see if the folder and database are already present, if they aren't it will go on the internet ( currently I am using dropbox to store the db file ) and download the database and store it on the sd card, then I it will open the database. The database is writable as it lets the user rate an object. I have two questions.
1.) I would love to provide updates to the database and then have my app update the database if the version number is higher and replace the existing one. I have done some research and from what I have found it is possible to store an xml or json file with the version number of and the just parse the information and if the version number is higher download the new database.
Can someone provide an example of how this is accomplished and whether it is better to use xml or json for this task?
2.) Is there a way to save the rating in the new version of the database when the new is downloaded and accessed?
Thanks
two nights ago I wrote something like that.
pack your database structure as an array in a webservice method by reading field names and field types. the structure of array is arbitrary.
call web service method and you must receive a string that represent a JSONArray object, if you sent it as json with json_encode() method in php.
read structure and make CREATE DB query string with for loops.
execute query, so you must have database.
also you can send alot of information with arrays.
introducing each part is hard, so for each part google it.
don't forget to convert field types to match SQLite types such as VARCHAR=>TEXT, smallint=>INTEGER , ...