Skip to content

Commit 371f723

Browse files
committed
- Refactored the update() method in the Recordset class to use prepared statements for both inserts and updates when usePreparedStatement == true
- Fixed minor bug in the constructor for the Date class. It wasn't setting the parserFailed flag correctly at the end of the method. git-svn-id: svn://192.168.0.80/JavaXT/javaxt-core@36 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent 2165691 commit 371f723

File tree

4 files changed

+70
-43
lines changed

4 files changed

+70
-43
lines changed

src/javaxt/sql/Connection.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ public boolean open(java.sql.Connection conn){
154154

155155
public void close(){
156156
try{Conn.close();}
157-
catch(Exception e){}
157+
catch(Exception e){
158+
e.printStackTrace();
159+
}
158160
}
159161

160162

src/javaxt/sql/Database.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package javaxt.sql;
22
import java.sql.ResultSet;
33
import java.sql.DatabaseMetaData;
4-
import java.sql.DriverManager;
4+
//import java.sql.DriverManager;
55

66
//******************************************************************************
77
//** Database
@@ -21,7 +21,7 @@ public class Database {
2121
private String password;
2222
private Driver driver;
2323
//private String url;
24-
private String path;
24+
//private String path;
2525
private String props;
2626

2727
private java.sql.Connection Connection = null;
@@ -63,13 +63,25 @@ public class Database {
6363

6464
};
6565

66-
66+
/** Microsoft SQL Server database driver. */
6767
public static Driver SQLServer = findDriver("SQLServer");
68+
69+
/** IBM DB2 database driver. */
6870
public static Driver DB2 = findDriver("DB2");
71+
72+
/** Sybase ASE database driver. */
6973
public static Driver Sybase = findDriver("Sybase");
74+
75+
/** PostgreSQL database driver. */
7076
public static Driver PostgreSQL = findDriver("PostgreSQL");
77+
78+
/** Derby database driver. */
7179
public static Driver Derby = findDriver("Derby");
80+
81+
/** SQLite database driver. */
7282
public static Driver SQLite = findDriver("SQLite");
83+
84+
/** Microsoft Access database driver. */
7385
public static Driver Access = findDriver("Microsoft Access");
7486

7587
public static Driver FrontBase = findDriver("FrontBase");

src/javaxt/sql/Recordset.java

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -560,25 +560,13 @@ else if (driver.equals("PostgreSQL")){
560560
}
561561

562562
}
563-
}
564-
565-
566-
567-
//Commit changes
568-
if (InsertOnUpdate){
569-
570-
/*
571-
if (usePreparedStatement){
572-
insertRecord();
573-
}
574-
else{
575-
rs.insertRow();
576-
}
577-
*/
578563

564+
rs.updateRow();
565+
}
566+
else{
579567

580568

581-
java.util.Vector<String> cols = new java.util.Vector<String>();
569+
java.util.ArrayList<String> cols = new java.util.ArrayList<String>();
582570
for (int i=0; i<Fields.length; i++){
583571
if (Fields[i].RequiresUpdate){
584572
cols.add(Fields[i].getName());
@@ -588,21 +576,38 @@ else if (driver.equals("PostgreSQL")){
588576
int numUpdates = cols.size();
589577

590578
StringBuffer sql = new StringBuffer();
591-
sql.append("INSERT INTO " + Fields[0].getTable() + " (");
592-
for (int i=0; i<numUpdates; i++){
593-
sql.append(cols.get(i));
594-
if (numUpdates>1 && i<numUpdates-1){
595-
sql.append(",");
579+
580+
if (InsertOnUpdate){
581+
sql.append("INSERT INTO " + Fields[0].getTable() + " (");
582+
for (int i=0; i<numUpdates; i++){
583+
sql.append(cols.get(i));
584+
if (numUpdates>1 && i<numUpdates-1){
585+
sql.append(",");
586+
}
587+
}
588+
sql.append(") VALUES (");
589+
for (int i=0; i<numUpdates; i++){
590+
sql.append("?");
591+
if (numUpdates>1 && i<numUpdates-1){
592+
sql.append(",");
593+
}
596594
}
595+
sql.append(")");
597596
}
598-
sql.append(") VALUES (");
599-
for (int i=0; i<numUpdates; i++){
600-
sql.append("?");
601-
if (numUpdates>1 && i<numUpdates-1){
602-
sql.append(",");
597+
else{
598+
sql.append("UPDATE " + Fields[0].getTable() + " SET ");
599+
for (int i=0; i<numUpdates; i++){
600+
sql.append(cols.get(i));
601+
sql.append("=?");
602+
if (numUpdates>1 && i<numUpdates-1){
603+
sql.append(", ");
604+
}
605+
}
606+
String where = new Parser(this.sqlString).getWhereString();
607+
if (where!=null){
608+
sql.append(" WHERE "); sql.append(where);
603609
}
604610
}
605-
sql.append(")");
606611
//System.out.println(sql);
607612

608613
java.sql.PreparedStatement stmt = Conn.prepareStatement(sql.toString(), java.sql.Statement.RETURN_GENERATED_KEYS);
@@ -641,25 +646,32 @@ else if (driver.equals("PostgreSQL")){
641646
stmt.setObject(id, FieldValue.toObject() );
642647

643648
if (FieldValue!=null){
644-
if (FieldValue.toObject().getClass().getPackage().getName().startsWith("javaxt.geospatial.geometry")){
645-
stmt.setObject(id, getGeometry(FieldValue));
649+
try{
650+
if (FieldValue.toObject().getClass().getPackage().getName().startsWith("javaxt.geospatial.geometry")){
651+
stmt.setObject(id, getGeometry(FieldValue));
652+
}
646653
}
654+
catch(Exception e){}
647655
}
648656

649657
id++;
650658
}
651659
}
652660
stmt.executeUpdate();
653-
654-
java.sql.ResultSet generatedKeys = stmt.getGeneratedKeys();
655-
if (generatedKeys.next()) {
656-
this.GeneratedKey = new Value(generatedKeys.getString(1));
661+
662+
663+
664+
if (InsertOnUpdate){
665+
java.sql.ResultSet generatedKeys = stmt.getGeneratedKeys();
666+
if (generatedKeys.next()) {
667+
this.GeneratedKey = new Value(generatedKeys.getString(1));
668+
}
669+
670+
InsertOnUpdate = false;
657671
}
658672

659-
InsertOnUpdate = false;
660-
}
661-
else{
662-
rs.updateRow();
673+
674+
//stmt.close();
663675
}
664676
}
665677
catch(Exception e){

src/javaxt/utils/Date.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public Date(long milliseconds){
7979
//**************************************************************************
8080
//** Constructor
8181
//**************************************************************************
82-
/** Creates a new instance of date using a String represention of a date */
83-
82+
/** Creates a new instance of date using a String representation of a date.
83+
*/
8484
public Date(String date){
8585
String[] Format = new String[]
8686
{
@@ -164,6 +164,7 @@ public Date(String date){
164164

165165
if (d==null){ //throw exception?
166166
currDate = new java.util.Date();
167+
parserFailed = true;
167168
}
168169

169170

0 commit comments

Comments
 (0)