Form duplication of field, before MySQL posting of data - java

I have almost managed to fix a long standing problem I've been having with duplicate fields in my website, today I got a very quick solution to make the column data unique. An easy fix that had escaped me.
However this leaves an auto increment entry to the database, with null values except for ID (primary column). You can see the effect this has in the website, LostDots.com
I have tried all the INSERT IGNORE options, and this didn't fix it either.
So I was thinking if it might be possible to use the validate form coding to eliminate entires before it gets sent to MySQL. i.e.
if (theForm.Editbox2.value == "")
{
alert("at least put a X");
theForm.Editbox2.focus();
return false;
}
if (theForm.Editbox2.value.length < 1)
{
alert("at least put a X");
theForm.Editbox2.focus();
return false;
}
As an example of what I was thinking, maybe if I pulled up the variable for the Editbox2 value, and got it to compare?
if (theForm.Editbox2.value = $namevariable)
{
alert("That domain name has already been advertised !!");
theForm.Editbox2.focus();
return false;
}
Having previously queried the present table data
(SELECT EDITBOX2 FROM TABLE1 WHERE NOT EXISTS $namevariable);
I am only wondering if this could be possible, I think getting the variable into Java is the problem? Maybe I haven't slept enough and speaking jibberish.
Let me know if it might be possible. Thanks, Alastair.

Related

Inserting NULL for NUMBER column

I am trying to insert NULL value for my NUMBER column in H2 database.
In my Java model the corresponding value is Long (not primitive). Now, I am aware how JDBC driver inserts 0 for nulls if not specified, but I do exactly that.
My DAO layer is in Spring, and for this exact column I am mapping its value in this manner.
if(user.getDefaultNumber() == null){
map.addValue("defaultNumber", user.getDefaultNumber(), Types.NULL);
}else{
map.addValue("defaultNumber", user.getDefaultNumber());
}
However, this does not seem to be working. I have tracked it deep into Spring Core classes, and null value for my object is there, but somewhere it still gets turned to 0.
Can someone please help me, this is bugging me for days.
Please try PreparedStatement to save null value in integer column
pst.setNull(4, java.sql.Types.INTEGER);
enter link description here
The solution to the problem was adding Types.Integer. I am still not sure why it did not work with Types.NULL but I will investigate. For now this is the solution
if(user.getDefaultNumber() == null){
map.addValue("defaultNumber", user.getDefaultNumber(), Types.INTEGER);
}else{
map.addValue("defaultNumber", user.getDefaultNumber());
}
Also, it is very important to map it properly when reading from DB. JDBC will read NULLs from database, but it will still insert 0 in Java's attribute. So, if reading is not handled properly as well, it might leave an impression that the problem persists even though the writing part is actually OK.

SQL JAVA EE WHERE statement isn't taken into account

I connect to a database with something like 50 tables, referenced in a table inside the database. The problem I'm having, is for one of these table, the WHERE statement in the SQL query is simply not taken into account by the query, like it was inexistent. I tried a lot of thing, like using row index instead of row name to reference it in the query. I searched for a while but nobody seems to have had the same problem.
It's strange because it happens in only one query, all the other work just fine, on the same type of data, same WHERE. So the solution is probably dumb, but at this point I need another point of view.
The second query below works just fine, but it's just as if the WHERE statement wasn't there... But the first one achieve is goal as intended.
resultCompare = compareStat.executeQuery("SELECT Nb_doc_Notes, Nb_doc_SQL FROM Comparaison_Replique_Resultats WHERE Table_SQL_associee = '"+tableName+"' ORDER BY ID_Comparatif DESC;");
resultCompare.next();
int numberLines = resultCompare.getInt("Nb_doc_Notes");
numberEntries = resultCompare.getInt("Nb_doc_SQL");
resultCompare.close();
resultCompare = compareStat.executeQuery("SELECT ID_Comparatif FROM Comparaison_Replique_Resultats WHERE Difference = "+REQUIRED_DIFFERENCE+" ORDER BY ID_Comparatif DESC;");
resultCompare.next();
int ID = resultCompare.getInt("ID_Comparatif");
resultCompare.close();
So when I check the "ID_Comparatif", it just get the last one in the table, ignoring the fact that Difference is almost everytime different from REQUIRED_DIFFERENCE (equals to 0 BTW, it's just that during my preliminary testing I tried putting the value in a variable and then concatenating it, just like in others query I have in the program.
Thanks !

Hibernate method to check existence or row in table but not return row

I need to implement a DAO method to check existence of a row in a table and return a boolean value accordingly. A crude version is shown:
public boolean check(Integer bookId) {
logger.info(BookDAOImpl.class.getName() + ".check() method called.");
return bookId.equals(this.get(bookId).getBookId());
}
I don't need to return the row found.
As this is only for a demo application, I'm using:
#Override
public boolean check(Integer bookId) {
logger.info(PersonDAOImpl.class.getName() + ".check() method called.");
try {
Book book = this.get(bookId);
return bookId.intValue() == book.getBookId().intValue();
}
catch(IndexOutOfBoundsException e) {
return false;
}
}
I guess you are trying to optimize the check in the interest of performance.
Let me clarify like this:
There are three steps in getting a row:
1. Making a method call/request over the network
2. Oracle scanning and finding the row(s) that match (with or without
index)
3. Copying the row data and bringing them back
The costs associated with (1) and (2) are huge, whereas the cost of (3) is small.
So, IMHO there is no point in trying to optimize the amount of data returned.
For example, if it takes 120ms just for checking, it may take only 125ms for getting the data.
There will hardly be 5% difference in normal cases.
Note:
- If your WHERE condition uses indexes, (2) will also become less
costly
- If you are selecting 50 to hundreds of columns, then it makes sense to find a way to do this.
Suggestion:
Unless you are selecting 50+ columns, don't bother. Just get the full data and check resultList.size().
Simply query the database using hibernate with bookId. If any resultset is returned return true or else false.

How to determine if a table contains a value in SQL?

I feel like I'm missing something very obvious here, but it seems that the only way to go about doing this is to get the value, and then see if it returns a null (empty) value, which I would rather not do.
Is there an equivalent to List.contains(Object o) in SQL? Or perhaps the JDBC has something of that nature? If so, what is it?
I am using Microsoft Access 2013.
Unfortunately I don't have any useful code to show, but here is the gist of what I am trying to do. It isn't anything unique at all. I want to have a method (Java) that returns the values of a user that are stored in the database. If the user has not previously been added to the database, the user should be added, and the default values of the user should be set. Then those newly created values will be returned. If a player has already been added to the database (with the username as the primary key), I don't want to overwrite the data that is already there.
I would also advise against using MS Access for this purpose, but if you are familiar with MS Office applications, the familiar UI/UX structure might help you get your footing and require less time to learn other database environments. However, MS Access tends to be quite limited, and I would advise considering alternative options if available.
The only way to see if an SQL table contains a row with some condition on a column is to actually make an SQL query. I don't see why you wouldn't do that. Just make sure that you have an index on the column that you will be constraining the results on. Also for better speed use count to prevent from retrieving all the data from the rows.
SELECT count(*) FROM foos WHERE bar = 'baz'
Assuming you have an index on the bar column this query should be pretty fast and all you have to do is check whether it returns > 0. If it does then you have rows matching your criteria.
You can use "IF EXISTS" which returns a boolean value of 1 or 0.
select
if(
exists( select * from date1 where current_date()>now() ),
'today > now',
'today is not > now'
) as 'today > now ?' ;
+--------------------+
| today > now? |
+--------------------+
| today is not > now |
+--------------------+
1 row in set (0.00 sec)
Another Example:
SELECT IF(
EXISTS( SELECT col from tbl where id='n' ),
colX, colY
) AS 'result'
FROM TBL;
I'm also new to sql and I'm using Oracle.
In Oracle, suppose we have: TYPE: value.
We can use:
where value not in (select TYPE from table)
to make sure value not exist in the column TYPE of the table.
Don't know if it helps.
You can simply use Query with condition.
For example if you have to check records with particular coloumn, you can use where condition
select * from table where column1 = 'checkvalue'
You can use count property to check the no. of records existing with your specified conditon
select count(*) from table where column1 = 'checkvalue'
I have created the following method, which to my knowledge works perfectly. (Using the java.sql package)
public static containsUser(String username)
{
//connection is the Connection object used to connect to my Access database.
Statement statement = this.connection.createStatement();
//"Users" is the name of the table, "Username" is the primary key.
String sql = "SELECT * FROM Users WHERE Username = '" + username + "'";
Result result = statement.executeQuery(sql);
//There is no need for a loop because the primary key is unique.
return result.next();
}
It's an extremely simple and extremely basic method, but hopefully it might help someone in the future.
If there is anything wrong with it, please let me know. I don't want anyone learning from or using poorly written code.
IMPORTANT EDIT: It is now over half a decade after I wrote the above content (both question and answer), and I now advise against the solution I illustrated above.
While it does work, it prioritizes a "Java-mindset-friendly" approach to SQL. In short, it is typically a bad idea to migrate paradigms and mindsets of one language to another, as it is inevitable that you will eventually find yourself trying to fit a square peg into a round hole. The only way to make that work is to shave the corners off the square. The peg will then of course fit, but as you can imagine, starting with a circle peg in the first place would have been the better, cleaner, and less messy solution.
Instead, refer to the above upvoted answers for a more realistic, enterprise-friendly solution to this problem, especially as I imagine the people reading this are likely in a similar situation as I was when I originally wrote this.

How to retrieve previously auto-generated PK ID value using JDBC and HSQLDB

I'm working with JDBC and HSQLDB 2.2.9. What's the most efficient and accurate way to insert a new row into a DB and, subsequently, retain its id (PK set to autoincrement) value? The reason I need to do this is probably pretty obvious, but I'll illustrate with an example for discussion:
Say there's a Customer table that has a PersonId field with a FK constraint referring to a row from a Person table. I want to create a new Customer, but to do this I need to first create a new Person and use the new Person.id value to set Customer.PersonId.
I've seen four ways to approach this:
Insert the Person row setting the id field to null. HSQLDB generates the next id value automatically. Then perform a query on the Person table to get the id value just created and use it to create the new Customer row.
This seems expensive just to retrieve a single integer value.
Get the next id value in the Person table and use it in the INSERT statement to set the Person.id value manually. Use the same id value to set Customer.PersonId. No subsequent read from the DB is needed.
Inconsistencies may arise if an id value is obtained, but another connection performs an INSERT in the table before my INSERT INTO Person... statement is executed.
Execute the INSERT statement, as in option 1 above, setting id=null to allow auto-generation. Then use the getGeneratedKeys method to retrieve keys generated in last statement.
I thought this sounded like a good option, but I couldn't get it to work. Here's a snippet of my code:
// PreparedStatement prepared previously...
preparedStatement.executeUpdate();
ResultSet genKeys = preparedStatement.getGeneratedKeys();
int id;
if (genKeys.next()) {
id = genKeys.getInt(1);
}
// Finish up method...
This code was returning an empty ResultSet for genKeys. Am I using the getGeneratedKeys method incorrectly? If I could get this to work, this might be the way to go.
Again, execute the INSERT statement allowing for auto-generated id. Then immediately execute CALL IDENTITY() to retrieve the last id value generated by the connection (as explained here and mentioned in this SO question).
This also seems like a reasonable option, even though I must perform an additional executeQuery. On the positive side, I was actually able to get it to work with the following code:
// INSERT statement executed here...
statement = connection.createStatement();
ResultSet rs = statement.executeQuery("CALL IDENTITY();");
int id;
if (rs.next()) id = rs.getInt(1);
// Finish up method...
So, in summary, the first two options I'm not crazy about. The second two seem ok, but I could only get option 4 to work. Which option is preferred and why? If option 3 is the best, what am I doing wrong? Also, is there a better way that I haven't mentioned? I know words like 'better' can be subjective, but I'm working with a simple DB and want the most direct solution that doesn't open up the DB to possible inconsistencies and doesn't increase the transaction failure rate (due to trying to create a record with an id that already exists).
This seems like a basic question (and essential), but I couldn't find much guidance on the best way to do it. Thanks.
EDIT:
I just found this question that discusses my option 3. According to the accepted answer, it appears I was leaving out the Statement.RETURN_GENERATED_KEYS parameter needed to enable that functionality. I didn't show the prepareStatement method in my code snippet, but I was using the single parameter version. I need to retry using the overloaded, two-parameter version.
There are also a few other SO questions which show up with that question that are closly related to my question. So, I guess mine could be considered a duplicate (not sure how I missed those other questions before). But I'd still like any guidance on whether one solution is considered better than the others. For now, if I get option 3 to work, I'll probably go with that.
I don't have enough reputation to comment on neizan's answer, but here's how I solved the same problem:
The column looked like an ID column, but it wasn't defined as IDENTITY;
As said above, you need to specify RETURN_GENERATED_KEYS.
It looks like if you execute 2 INSERT in sequence, the second one won't return the generated keys. Use "CALL IDENTITY()" instead.
Example using HSQLDB 2.2.9:
CREATE TABLE MY_TABLE (
ID INTEGER IDENTITY,
NAME VARCHAR(30)
)
Then in Java:
PreparedStatement result = cnx.prepareStatement(
"INSERT INTO MY_TABLE(ID, NAME) VALUES(NULL, 'TOM');",
RETURN_GENERATED_KEYS);
int updated = result.executeUpdate();
if (updated == 1) {
ResultSet generatedKeys = result.getGeneratedKeys();
if (generatedKeys.next()) {
int key = generatedKeys.getInt(1);
}
}
Not much action here, so I'll go ahead and answer to bring closure to this question. After playing around with the different options, and after see this question, I was able to get my option 3 to work. Like I mentioned in the edit to my question, I'm going to use option 3. Option 4 also worked fine, but since the accepted answer to the linked question is given by a reputable source, I am sticking with that. I wish I'd have seen that question/answer before starting this one, I'd have saved some time!

Categories