Netbeans jTable custom WHERE clause - java

Something simple I guess... I have a jTable created by Netbeans, which is bound to a mysql DB.
The table is getting all the data from the table, but I only need to get data with a particular field value. Netbeans creates a class automatically, but I can't find a place to add a WHERE clause.
For example I need the table to execute the final query as, SELECT * WHERE ID = 123;

select * where ID = 123
is incorrect as a query. You should research first, in 30 seconds I've found this, which I believe solves the problem.

Related

Position Autoincrement in Talend

So i a bit lost and don t really know how to hang up this one...
Consider that i have a 2 DB table in Talend, let say firstly
A table invoices_only which has as fields, the invoiceNummer and the authors like this
Then, a table invoices_table with the field (invoiceNummer, article, quantity and price) and for one invoice, I can have many articles, for example
and through a tmap want to obtain a table invoice_table_result, with new columns, one for the article position, an one other for the total price. for the position i know that i can use something like the Numeric.sequence("s1",1,1) function, but don t know how to restart my counter when a new invoices nummer is found, and of course for the total price it is just a basic multiplication
so my result should be some thing like this
Here is a draft of my talend job, i m doing a lookup on the invoicenummer between the table invoice_only and invoices
Any Advices? thanks.
A trick I use is to do the sequence like this:
Numeric.sequence("s" + row.InvoiceNummer, 1, 1)
This way, the sequence gets incremented while you're still on the same InvoiceNummer, and a new one is started whenever a new InvoiceNummer is found.
There are two ways to achieve it,
tJavaFlex
Sql
tJavaFlex
You can compare current data with the previous data and reset the sequence value using below function,
if () {
Numeric.resetSequence(seqName, startValue);
}
Sql
Once data is loaded into the tables, create a post job and use an update query to update the records. You have to select the records and take the rank of the values. On top of the select you have to perform the update.
select invoicenumber, row_number() over(partition by invoicenumber, order by invoicenumber) from table name where -- conditions if any.
Update statements vary with respect to the database, please provide which database are you using, so that can provide the update query.
I would recommend you to achieve this through Sql

Auto Create MySQL Tables on First Run Java/MySQL

I am trying to Auto create SQL tables on first run, and haven't found any good tutorial on google. Does anyone have a suggestion or can explain it to me. My class so far looks like: http://pastebin.com/6u8yFWrt
In Mysql you can do:
CREATE TABLE tablename IF NOT EXISTS...
This will, as the name says, create the table if there is no table with the same name, described here. If you run it whenever you open the connection to the database, you should be fine.
BUT this is no guarantee that the existing table has the format you want! If you change the definition of the table halfway through the process, because you want an extra column, you will need to delete the existing table fist, for the changes in the query to have an effect.
I think you should use JPA and Hibernate instead. Youre probably in for quite a journey until you get the hold of it but I think it is worth the effort.

SQL insert or update through servlet

I am learning Java and have big problem with SQL query. I have field that should be updated if exist or inserted if not exist.
Firstly I tried with IF NOT EXISTS, but it turns out that Oracle doesn't support that.
Then I tried with EXCEPTION WHEN NO_DATA_FOUND, but it also failed.
After few hours of searching I found out that MERGE must work on my example, but for some reason I couldn't make it to work. I have never used merge before, so maybe I made mistake in query, but I don’t think that’s the problem.
So, I have table commercial and field in it idArt, idPla, quan and ID (unique and auto increment). From html form I am sending first three values to servlet and then servlet should do next thing:
if there is row with idArt=(formIdArt) AND idPla=(formIdPla) then update quan=quan+(formQuan), ELSE insert all three values.
This looks simple, but I have already lost way too much time on this, so can please someone help me with this?
First,
Try to update your table like this:
update commercial set quan = quan + formquan where idArt=formIdArt and idPla = formIdPla
if execution of this query returns result as more than 0 row affected then do not insert all three values, else insert all three values in your table

Is it possible to call database trigger from Java code?

I searched a bit and I found some info on how to call java code from database trigger but none about the opposite. Isn't it possible to call a trigger from a java method?
After inserting to one table (Table1), I need to create several rows to another table (Table2) from a select on the first one. I built the trigger but if I make it to execute after insert on the first table I get an error:
ORA-04091: table Table1 is mutating, trigger/function may not see it ORA-06512:....
I am working on an ADF application, and as Table1-Table2 have a master detail relationship, maybe it doesn't allow inserting rows this way. That is why I thought that calling the trigger through a button may solve my problem. Any idea?
Trigger:
CREATE OR REPLACE TRIGGER Table2
AFTER INSERT ON Table1 REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
insert into Table2
(
Select col1,col2... from Table1
);
END;
Triggers can't be called directly, they are only executed before/after rows are inserted/updated/deleted.
The problem is that in your trigger you are trying to access the table for which you have created your trigger, and that results in the mutating trigger error, because the trigger won't see the changes in the table.
Can you show use the code of your trigger? Why do you have to access that table? Maybe you could just use a stored procedure to achieve what you need and call it?
Edit
If you want to access the values from inserted row, you should just use the :new pseudorecord:
CREATE OR REPLACE TRIGGER Table2
AFTER INSERT ON Table1 REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
insert into Table2 values (:new.some_col, :new.some_col2, <other columns>);
END;
For calling your treatment you can create a PL/SQL Procedure/function and copy/paste (modulo some modifications) your trigger code in this procedure.
From what i understand, no insert should complete until the trigger has been completed perhaps there is an issue with the insert statement?

Java JDBC UPDATE Add Number?

So let's say I have an int column in MySQL. I want to update that column by adding to it, without running a SELECT query to get the number and add it. Is this possible?
update tablename set field = field + 1 where condition
This is a direct MySql update command to do so. You didn't mention if you were using a specific ORM like Hibernate or anything, but this same concept can be applied in HQL etc.

Categories