218 lines
6.0 KiB
Java
218 lines
6.0 KiB
Java
package tools;
|
|
|
|
//import org.ccalm.main.AcceptASDCController;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Types;
|
|
import java.text.DateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
public class PreparedStatementNamed {
|
|
private static final Logger logger = LoggerFactory.getLogger(PreparedStatementNamed.class);
|
|
|
|
private class HMap{
|
|
public String name = "";
|
|
public int pos = -1;
|
|
public HMap(String name,int pos) {
|
|
this.name = name;
|
|
this.pos = pos;
|
|
}
|
|
}
|
|
|
|
private List< HMap > fields = new ArrayList< HMap >();
|
|
private PreparedStatement m_prepStmt;
|
|
|
|
public PreparedStatementNamed(Connection conn, String sql) throws SQLException {
|
|
int cnt=0;
|
|
int pos = 0;
|
|
while((pos = sql.indexOf("${")) != -1) {
|
|
int end = sql.substring(pos).indexOf("}");
|
|
if (end == -1)
|
|
end = sql.length();
|
|
else
|
|
end += pos+1;
|
|
cnt++;
|
|
fields.add(new HMap(sql.substring(pos+2,end-1),cnt));
|
|
sql = sql.substring(0, pos) + "?" + sql.substring(end); //Removing a parameter from a string
|
|
}
|
|
m_prepStmt = conn.prepareStatement(sql);
|
|
}
|
|
public void setString(String name, String value) throws SQLException {
|
|
for(int i=0;i<fields.size();i++) {
|
|
if(fields.get(i).name.equals(name)) {
|
|
m_prepStmt.setString(fields.get(i).pos, value);
|
|
}
|
|
}
|
|
}
|
|
public void setNULLString(String name) throws SQLException {
|
|
for(int i=0;i<fields.size();i++) {
|
|
if(fields.get(i).name.equals(name)) {
|
|
m_prepStmt.setNull(fields.get(i).pos,Types.NULL);
|
|
}
|
|
}
|
|
}
|
|
public void setInt(String name, String value) throws SQLException {
|
|
if(value==null){
|
|
setNULLInt(name);
|
|
}else {
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
if (fields.get(i).name.equals(name)) {
|
|
m_prepStmt.setInt(fields.get(i).pos, Integer.parseInt(value));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public void setInt(String name, int value) throws SQLException {
|
|
for(int i=0;i<fields.size();i++) {
|
|
if(fields.get(i).name.equals(name)) {
|
|
m_prepStmt.setInt(fields.get(i).pos, value);
|
|
}
|
|
}
|
|
}
|
|
public void setNULLInt(String name) throws SQLException {
|
|
for(int i=0;i<fields.size();i++) {
|
|
if(fields.get(i).name.equals(name)) {
|
|
m_prepStmt.setNull(fields.get(i).pos,Types.INTEGER);
|
|
}
|
|
}
|
|
}
|
|
public void setBoolean(String name, String value) throws SQLException {
|
|
if(value==null){
|
|
setNULLBoolean(name);
|
|
}else {
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
if (fields.get(i).name.equals(name)) {
|
|
if (value.equals("0") || value.equals("false") || value.isEmpty())
|
|
m_prepStmt.setBoolean(fields.get(i).pos, false);
|
|
else
|
|
m_prepStmt.setBoolean(fields.get(i).pos, true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public void setBoolean(String name, int value) throws SQLException {
|
|
for(int i=0;i<fields.size();i++) {
|
|
if(fields.get(i).name.equals(name)) {
|
|
if(value==0)
|
|
m_prepStmt.setBoolean(fields.get(i).pos, false);
|
|
else
|
|
m_prepStmt.setBoolean(fields.get(i).pos, true);
|
|
}
|
|
}
|
|
}
|
|
public void setNULLBoolean(String name) throws SQLException {
|
|
for(int i=0;i<fields.size();i++) {
|
|
if(fields.get(i).name.equals(name)) {
|
|
m_prepStmt.setNull(fields.get(i).pos,Types.BOOLEAN);
|
|
}
|
|
}
|
|
}
|
|
/*private int getIndex(String name) {
|
|
size()
|
|
} */
|
|
public void setDouble(String name, String value) throws SQLException {
|
|
if(value==null){
|
|
setNULLDouble(name);
|
|
}else {
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
if (fields.get(i).name.equals(name)) {
|
|
m_prepStmt.setDouble(fields.get(i).pos, Double.parseDouble(value));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public void setDouble(String name, double value) throws SQLException {
|
|
for(int i=0;i<fields.size();i++) {
|
|
if(fields.get(i).name.equals(name)) {
|
|
m_prepStmt.setDouble(fields.get(i).pos, value);
|
|
}
|
|
}
|
|
}
|
|
public void setNULLDouble(String name) throws SQLException {
|
|
for(int i=0;i<fields.size();i++) {
|
|
if(fields.get(i).name.equals(name)) {
|
|
m_prepStmt.setNull(fields.get(i).pos,Types.DOUBLE);
|
|
}
|
|
}
|
|
}
|
|
public void setFloat(String name, String value) throws SQLException {
|
|
if(value==null){
|
|
setNULLFloat(name);
|
|
}else {
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
if (fields.get(i).name.equals(name)) {
|
|
m_prepStmt.setDouble(fields.get(i).pos, Float.parseFloat(value));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public void setFloat(String name, float value) throws SQLException {
|
|
for(int i=0;i<fields.size();i++) {
|
|
if(fields.get(i).name.equals(name)) {
|
|
m_prepStmt.setFloat(fields.get(i).pos, value);
|
|
}
|
|
}
|
|
}
|
|
public void setNULLFloat(String name) throws SQLException {
|
|
for(int i=0;i<fields.size();i++) {
|
|
if(fields.get(i).name.equals(name)) {
|
|
m_prepStmt.setNull(fields.get(i).pos,Types.FLOAT);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void setTimestamp(String name, String value) throws SQLException {
|
|
if(value==null){
|
|
for(int i=0;i<fields.size();i++) {
|
|
if(fields.get(i).name.equals(name)) {
|
|
m_prepStmt.setNull(fields.get(i).pos,Types.DATE, null);
|
|
}
|
|
}
|
|
}else {
|
|
if(value.matches("\\d+")) //If old date format in UNIX time
|
|
{
|
|
long uDate=Long.parseLong(value)+1;
|
|
java.sql.Timestamp tm=new java.sql.Timestamp(uDate*1000);
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
if (fields.get(i).name.equals(name)) {
|
|
m_prepStmt.setTimestamp(fields.get(i).pos, tm);
|
|
}
|
|
}
|
|
}else
|
|
{
|
|
java.sql.Timestamp tm=null;
|
|
DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //2016-05-29 18:00:01 in "GMT"
|
|
try{
|
|
tm = new java.sql.Timestamp(dfm.parse(value).getTime());
|
|
} catch (Exception ex) {
|
|
logger.error(ex.getMessage(), ex);
|
|
}
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
if (fields.get(i).name.equals(name)) {
|
|
m_prepStmt.setTimestamp(fields.get(i).pos, tm);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public PreparedStatement getPreparedStatement() {
|
|
return m_prepStmt;
|
|
}
|
|
/*public ResultSet executeQuery() throws SQLException {
|
|
return m_prepStmt.executeQuery();
|
|
}
|
|
public void close() throws SQLException {
|
|
m_prepStmt.close();
|
|
}*/
|
|
} |