I am trying to create the insertion method in SQL SERVER using gvSIG and Eclipse, but am getting this error, here's the log ( I think the getinsertFeature gets an empty charsetname):
Exception in thread "main" java.lang.NullPointerException: charsetName
at java.lang.String.<init>(String.java:441)
at java.io.ByteArrayOutputStream.toString(ByteArrayOutputStream.java:187)
at com.iver.cit.gvsig.fmap.drivers.jdbc.mssqlspatial.MicrosoftSqlSpatial.doubleQuote(MicrosoftSqlSpatial.java:162)
at com.iver.cit.gvsig.fmap.drivers.jdbc.mssqlspatial.MicrosoftSqlSpatial.addQuotes(MicrosoftSqlSpatial.java:145)
at com.iver.cit.gvsig.fmap.drivers.jdbc.mssqlspatial.MicrosoftSqlSpatial.getSqlInsertFeature(MicrosoftSqlSpatial.java:218)
at com.iver.cit.gvsig.fmap.drivers.jdbc.mssqlspatial.Main.main(Main.java:50)
here's also the doubleQuote and addQuotes methods: protected String addQuotes(Object value) { String retString;
if (value != null) {
if (value instanceof NullValue)
retString = "null";
else
retString = "'" + doubleQuote(value) + "'";
} else {
retString = "null";
}
return retString;
}
private String doubleQuote(Object obj) {
String aux = obj.toString().replaceAll("'", "''");
StringBuffer strBuf = new StringBuffer(aux);
ByteArrayOutputStream out = new ByteArrayOutputStream(strBuf.length());
PrintStream printStream = new PrintStream(out);
printStream.print(aux);
String aux2 = "ERROR";
try {
aux2 = out.toString(toEncode);
System.out.println(aux + " " + aux2);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return aux2;
}
Here's the getSqlInsertFeature method:
public String getSqlInsertFeature(DBLayerDefinition dbLayerDef,
IFeature feat) throws ProcessVisitorException {
StringBuffer sqlBuf = new StringBuffer(" USE " +dbLayerDef.getDataBase()+ "INSERT INTO "
+ dbLayerDef.getComposedTableName() + " (");
String sql = null;
int numAlphanumericFields = dbLayerDef.getFieldNames().length;
for (int i = 0; i < numAlphanumericFields; i++) {
String name = dbLayerDef.getFieldsDesc()[i].getFieldName();
// if (cols.contains(name) && (!name.equals(uniqueCol) ||
// existsUnique)) {
if (name.equals(dbLayerDef.getFieldID()))
continue;
sqlBuf.append(" " + "\""+name+"\"" + ",");
// }
}
sqlBuf.append(" " + dbLayerDef.getFieldGeometry());
// sqlBuf.deleteCharAt(sqlBuf.lastIndexOf(","));
sqlBuf.append(" ) VALUES (");
String insertQueryHead = sqlBuf.toString();
sqlBuf = new StringBuffer(insertQueryHead);
for (int j = 0; j < numAlphanumericFields; j++) {
String name = dbLayerDef.getFieldsDesc()[j].getFieldName();
if (name.equals(dbLayerDef.getFieldID()))
continue;
if (isNumeric(feat.getAttribute(j))){
sqlBuf.append(feat.getAttribute(j) + ", ");
}else if(feat.getAttribute(j).getSQLType() == Types.DATE){
//If the field is a date, the driver can not use the client encoding.
//It uses the same encoding that the user has written on the table
sqlBuf.append(addQuotes(((DateValue)feat.getAttribute(j)).getValue().toString()) + ", ");
}else{
sqlBuf.append(addQuotes(feat.getAttribute(j)) + ", ");
}
}
IGeometry geometry=feat.getGeometry();
int type=dbLayerDef.getShapeType();
if (geometry.getGeometryType()!=type){
if (type==FShape.POLYGON){
geometry=ShapeFactory.createPolygon2D(new GeneralPathX(geometry.getInternalShape()));
}else if (type==FShape.LINE){
geometry=ShapeFactory.createPolyline2D(new GeneralPathX(geometry.getInternalShape()));
}else if (type==(FShape.POLYGON|FShape.Z)){
geometry=ShapeFactory.createPolygon3D(new GeneralPathX(geometry.getInternalShape()),((IGeometry3D)geometry).getZs());
}else if (type==(FShape.LINE|FShape.Z)){
geometry=ShapeFactory.createPolyline3D(new GeneralPathX(geometry.getInternalShape()),((IGeometry3D)geometry).getZs());
}else if (type==(FShape.LINE|FShape.M)){ //MCoord
geometry=ShapeMFactory.createPolyline2DM(new GeneralPathX(geometry.getInternalShape()),((IGeometryM)geometry).getMs()); //MCoord
}
}
if (!isCorrectGeometry(geometry, type))
throw new ProcessVisitorException("incorrect_geometry",new Exception());
//MCoord
if ((type == (FShape.LINE|FShape.M)) || (type == (FShape.POINT|FShape.M))){
// stmt.execute("INSERT INTO testTable (id,name, Geom) VALUES (1,'a name', geometry::STGeomFromText('LINESTRING (100 100, 20 180, 180 180)', 0))");
sqlBuf.append(" geometry::STGeomFromText( '"
+ ((FShapeM)geometry.getInternalShape()).toText() + "', "
+ DefaultJDBCDriver.removePrefix(dbLayerDef.getSRID_EPSG()) + ")");
}else{
Geometry jtsGeom=geometry.toJTSGeometry();
if (jtsGeom==null || !isCorrectType(jtsGeom, type)){
throw new ProcessVisitorException("incorrect_geometry",new Exception());
}
sqlBuf.append(" geometry::STGeomFromText( '"
+ jtsGeom.toText() + "', "
+ DefaultJDBCDriver.removePrefix(dbLayerDef.getSRID_EPSG()) + ")");
}
// sqlBuf.deleteCharAt(sqlBuf.lastIndexOf(","));
sqlBuf.append(" ) ");
sql = sqlBuf.toString();
return sql;
}
and the main too (sorry it's too long, but I can't figure out the error):
public class Main {
/**
* @param args
* @throws ProcessVisitorException
*/
public static void main(String[] args) throws ProcessVisitorException {
DBLayerDefinition dbLayerDef =new DBLayerDefinition();
dbLayerDef.setDataBase("harhoura");
String[] fieldsDescr=new String[]{"id","nom","prenom","adresse"};
dbLayerDef.setFieldNames(fieldsDescr);
MicrosoftSqlSpatial m=new MicrosoftSqlSpatial();
dbLayerDef.setFieldID("gid");
dbLayerDef.setTableName("tableSpatial");
dbLayerDef.setFieldGeometry("the_geom");
Value[] value = new Value[2];
value[0] = ValueFactory.createValue("hh");
value[1] = ValueFactory.createValue("pp");
IGeometry geom = null;
String str="POLYGON((368549.975515418 380972.971955164,368563.061999346 380972.669377501,368561.170888952 380967.071690734,368555.346268938 380966.61782424,368543.470095662 380965.861380082,368532.274722128 380964.802358261,368532.04778888 380966.390890992,368531.896500049 380968.735867881,368531.745211217 380970.929555939,368531.669566801 380972.442444254,368531.669566801 380973.577110491,368549.975515418 380972.971955164,368549.975515418 380972.971955164,368549.975515418 380972.971955164,368549.975515418 380972.971955164))";
geom=getGeomFromDBJournal(str);
IFeature feat = new DefaultFeature(geom, value, "" + 3);
String query=m.getSqlInsertFeature(dbLayerDef, feat);
System.out.println(query);
}
public static IGeometry getGeomFromDBJournal(String str) {
IGeometry geom = null;
WKTParser wktParser = new WKTParser();
try {
geom = wktParser.read(str);
} catch (Exception e1) {
e1.printStackTrace();
}
return geom;
}
}
Thanks for help.