Does anyone know whether it is possible to execute spatialite functions on sqlite-databases in intellij?
Reference of spatialite functions: http://www.gaia-gis.it/gaia-sins/spatialite-sql-4.2.0.html
In particular, I would be interested in using functions on type Point.
Thanks!
Edit: Functions do work within the official spatialite-gui, however I don't think there is a way to use the spatialite-gui programmatically, is there?
Here is what I tried so far: In intellij I connected the Java JDBC library and tried using function ST_X(point) and ST_Y(point) with no success:
Connection c = null;
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:" + path + databaseName);
c.setAutoCommit(false);
System.out.println("Opened database \"" + databaseName + "\" successfully");
String sql = "SELECT id, ST_Y(point), ST_X(point) from tablename;";
Statement stmt = c.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while ( rs.next() ) {
String msg = "Id: ";
msg += rs.getInt(1);
msg += " , Latitude: ";
msg += rs.getDouble(2);
msg += " , Longitude: ";
msg += rs.getDouble(3);
System.out.println(msg);
}
rs.close();
stmt.close();
c.close();
This throws the following exception:
Exception in thread "main" java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such function: ST_Y)
at org.sqlite.core.DB.newSQLException(DB.java:890)
at org.sqlite.core.DB.newSQLException(DB.java:901)
at org.sqlite.core.DB.throwex(DB.java:868)
at org.sqlite.core.NativeDB.prepare(Native Method)
at org.sqlite.core.DB.prepare(DB.java:211)
at org.sqlite.jdbc3.JDBC3Statement.executeQuery(JDBC3Statement.java:81)
at com.company.Test.main(Test.java:77)
Edit 2:
I'm lost. It seems that I need to load extensions to make it work. Is this not included in the JDBC connector? (I pulled the JDBC connector through Maven.)
Where do I find the correct extensions?
Are those the ones? https://www.gaia-gis.it/fossil/libspatialite/index
Or those? http://www.gaia-gis.it/gaia-sins/index.html
How do I use them? Has anybody done this before?
Before performing spatial queries you need to load the spatialite module doing
SELECT load_extension('mod_spatialite');
You can find the documentation here: Dynamically loading SpatiaLite as an extension module.
Note that the module to load must be on the system path (documentation) and the path separator must be /. Also take into account that the spatialite module and Java must be compatibles (both 32 or 64 bits).
Related
When I run the code below my database gets created in the directory of the project this code is in. Now I have an application that runs on Ubuntu. The problem I'm running into is that in one class it can use the database and in another it cannot. Knowing where my database is saved will help my solve the problem. I already looked in the project directory on my Linux system but I couldn't find the file. It should also be mentioned that both classes use the database in basically the same way.
This is a demo of the code I use on Windows with no problems.:
public class Main{
public static void main(String[] args) throws SQLException {
String url = "jdbc:sqlite:" + "testdb";
Connection conn = DriverManager.getConnection(url);
if (conn != null) {
DatabaseMetaData meta = conn.getMetaData();
System.out.println("The driver name is " + meta.getDriverName());
}
Connection c = DriverManager.getConnection(url);
Statement stmt = c.createStatement();
String sql = "CREATE TABLE IF NOT EXISTS TEST " +
"(id INTEGER PRIMARY KEY AUTOINCREMENT," +
" TEST TEXT NOT NULL " +
") ";
stmt.execute(sql);
stmt.close();
}
The exception I get is the following:
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such table: WR_PMC_SIGNALS)
java.lang.NullPointerException
This is the same table that is accessed in a different class. I cannot find the file that contains the database anywhere. I think I could solve the issue if I could directly find the database.
Does anyone know how I can find the directory of the generated database? It's hard to reproduce the problem here as the system I'm working on is a Universal Robot.
I am porting a Tomcat7/Mysql applicationto AWS. I am seeing that whenever I send a multiple query statement I get an error like this:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ....
I have only detyected it on multiple queries (f.e. Multiple related deletes, like "delete from xxx; delete from xxx"). Single queries seem to work correctly.
I am using this code for the connection.
Class.forName("com.mysql.jdbc.Driver");
String jdbcUrl = "jdbc:mysql://" + hostname + ":" + port + "/" + dbName + "?user=" + userName + "&password=" + password + "&autoReconnect=true&allowMultiQueries=true";
Connection con = DriverManager.getConnection(jdbcUrl);
To run the query I have this code:
Statement st = conn.createStatement();
res = st.executeQuery(query);
conn.commit();
I have already set the allowMultiQueries=true but it is not working correctly. What am I doing wrong?
Is multiquery supported?
As pointed out by #jarmod, the problem was in the url and the separator. I should use & instead of &
&autoReconnect=true&allowMultiQueries=true
I wrote a Java stored procedure, packed it into a jar and installed it into the Teradata database. I want to use the default database connection as described here. Most of the code was generated by the Teradata wizard for stored procedures.
public class TestSql {
public static void getEntryById(int id, String[] resultStrings) throws SQLException {
Connection con = DriverManager.getConnection("jdbc:default:connection");
String sql = "SELECT x FROM TEST_TABLE WHERE ID = " + id + ";";
Statement stmt = (Statement) con.createStatement();
ResultSet rs1 = ((java.sql.Statement) stmt).executeQuery(sql);
rs1.next();
String resultString = rs1.getString(1);
stmt.close();
con.close();
resultStrings[0] = resultString;
}
}
I installed the jar:
CALL SQLJ.REPLACE_JAR('CJ!/my/path/Teradata-SqlTest.jar','test');
And created the procedure:
REPLACE PROCEDURE "db"."getEntryById" (
IN "id" INTEGER,
OUT "resultString" VARCHAR(1024))
LANGUAGE JAVA
MODIFIES SQL DATA
PARAMETER STYLE JAVA
EXTERNAL NAME 'test:my.package.TestSql.getEntryById(int,java.lang.String[])';
Now when I call this procedure, I get this error message:
Executed as Single statement. Failed [7827 : 39001] Java SQL Exception SQLSTATE 39001: Invalid SQL state (08001: No suitable driver found for jdbc:default:connection).
Now when I log off from Teradata and log on again and call the procedure, the error message becomes:
Executed as Single statement. Failed [7827 : 39001] A default connection for a Java Stored Procedure has not been established for this thread.).
What is the problem here? I'm connecting to Teradata using the Eclipse plugin. Teradata v. 15.0.1.01.
After many hours I finally found the problem. Eclipse packed all dependencies into the jar - which basically is ok. However it also packed the Teradata JDBC driver files (tdgssconfig.jar and terajdbc4.jar) into the result jar, which was the problem.
I adjusted the jar building process so that these files are not included and the errors went away.
I am trying to get Generated key from sequnce.(Using Servlets & Oracle10)
Following is my code:
query ="insert into TABLE_NAME(COL1,COL2,COL3) values(sysdate,?,SEQ_NAME.nextval)";
PreparedStatement pstmt = con.prepareStatement(query,new String[]{"COL3"}); //Getting error on this line
pstmt.setString(1,Str2);
pstmt.executeUpdate();
ResultSet keyset = pstmt.getGeneratedKeys();
if(keyset.next())
{
genKey = keyset.getString(1);
}
But I am getting the Exception:
java.sql.SQLException: Unsupported feature
Few days ago this code was working fine. So what might be the reason that this code is not working now? (I haven't changed the JDBC driver war file)
Thanks in advance.
Is there another way of getting the value generated by sequence in the query?
Download the latest JDBC drivers, I think you need at least the 10.2.0.1 drivers and the db also need to be 10.2+
http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc101040.html
or first get the sequence value
String sqlForSeq = "select SEQ_NAME.NEXTVAL from dual";
ResultSet rs = stmt.executeQuery(sqlForSeq);
if (rs.next()) {
logSeq = rs.getString("NEXTVAL");
}
I have tried several examples , but none of them seem to work. Here is the code I tried last
import oracle.jdbc.driver.OracleDriver;
PreparedStatement prs = null;
ResultSet rrs = null;
Class stmt1 = null;
java.lang.reflect.Field mem = null;
requestSQL = "Select FIPS_STATE_CD_TXT, FIPS_COUNTY_CD_TXT from MSTR_FIPS_COUNTY where STATE_ID = ? " + " and COUNTY_TXT = ?";
prs.setString(1, vPropertyState);
prs.setString(2, vPropertyCounty);
System.out.println(prs.toString()); //JRN
Class stmt1 = prs.getClass();
java.lang.reflect.Field mem = stmt1.getField("sql");
String value= (String)mem.get(prs);
rrs = prs.executeQuery();
I get an error on this at :
Exception trying to make a TAF call
java.lang.NoSuchFieldException: sql
at java.lang.Class.getField(Class.java:1520)
I even tried using this example from JavaWorld, but my compiler doesn't seem to recognize DebugLevel and StatementFactory. Is there a special package I should download for this?
http://www.javaworld.com/javaworld/jw-01-2002/jw-0125-overpower.html?page=3
I am using Java 1.6 and Oracle 11g. I am also looking for a quick fix, rather than installing log4jdbc or p6sy
Different drivers use different names. In your case the sql field you are trying to access is not one of the available ones for that driver.
To get all the names of your JDBC driver use this code:
Class stmt1 = prepStmt.getClass();
try {
java.lang.reflect.Field mem[] = stmt1.getDeclaredFields();
for (Field x:mem){
System.out.println("Field:"+x.getName());
}
} catch (SecurityException ex) {
}
Observe the field name, and then use your code above to print its value.
The answer is, you can't. Atleast not for the Oracle jdbc driver.
What you are trying to achieve is not possible for oracle jdbc driver.
But in general some drivers (mysql) supports this
prs.toString()