我一直试图让这个例子让activejdbc工作无济于事。我的数据库中有表格来使用这段代码,这就是它在我的编辑器中打开时的样子(intellij):
我删除了一些未被此代码使用的表,因为它只是为了测试不同的项目,而这些表对于这个问题并不重要。
我正在尝试运行的代码如下:
package activejdbc.examples.simple;
import org.javalite.activejdbc.Base;
import org.javalite.activejdbc.DB;
import org.postgresql.Driver;
public class Main {
public static void main(String[] args) {
try {
System.out.println(Class.forName("org.postgresql.Driver"));
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? "
+ "Include in your library path!");
e.printStackTrace();
return;
}
Base.open("org.postgresql.Driver", "jdbc:postgresql://<ip>:<port>/<database_name>", "<database_user>", "<password>");
Person director = new Person();
director.set("name", "Steven Spielberg");
director.saveIt();
director.add(new Movie("Saving private Ryan", 1998));
director.add(new Movie("Jaws", 1982));
director.getAll(Movie.class).forEach(System.out::println);
Base.close();
}
}
根据http://javalite.io/activejdbc#supported-databases
对象(Movie,MoviesPeople,Person)都是空类这会在运行时出现以下错误:
Connected to the target VM, address: '127.0.0.1:42363', transport: 'socket'
class org.postgresql.Driver
Exception in thread "main" org.javalite.activejdbc.InitException: java.lang.NullPointerException
at org.javalite.activejdbc.Registry.init(Registry.java:154)
at org.javalite.activejdbc.Registry.getMetaModel(Registry.java:103)
at org.javalite.activejdbc.ModelDelegate.metaModelOf(ModelDelegate.java:289)
at org.javalite.activejdbc.Model.<init>(Model.java:79)
at activejdbc.examples.simple.Person.<init>(Person.java:10)
at activejdbc.examples.simple.Main.main(Main.java:25)
Caused by: java.lang.NullPointerException
at org.postgresql.jdbc2.AbstractJdbc2DatabaseMetaData.getColumns(AbstractJdbc2DatabaseMetaData.java:2502)
at org.postgresql.jdbc4.AbstractJdbc4DatabaseMetaData.getColumns(AbstractJdbc4DatabaseMetaData.java:99)
at org.javalite.activejdbc.Registry.fetchMetaParams(Registry.java:198)
at org.javalite.activejdbc.Registry.init(Registry.java:138)
... 5 more
Disconnected from the target VM, address: '127.0.0.1:42363', transport: 'socket'
似乎永远不会建立数据库连接,并且无法从activejdbc创建/调用对象。但我不知道这里出了什么问题,我觉得它应该有效,我知道我的数据库使用的url是正确的,因为它在我的编辑器中可以连接到数据库。而且司机显然在那里工作,因为我的检查不会失败。有什么建议吗?
PS:这是我的pom.xml,如果这有助于解决这个问题:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.heroku.sample</groupId>
<artifactId>webappRunnerSample</artifactId>
<version>1.0-SNAPSHOT</version>
<name>webappRunnerSample Maven Webapp</name>
<packaging>war</packaging>
<url>http://maven.apache.org</url>
<properties>
<activejdbc.version>2.1-SNAPSHOT</activejdbc.version>
<environments>development.test,development</environments>
</properties>
<build>
<finalName>webappRunnerSample</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.javalite</groupId>
<artifactId>activejdbc-instrumentation</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>instrument</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.javalite</groupId>
<artifactId>db-migrator-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<configFile>
${project.basedir}/src/main/resources/database.properties
</configFile>
<environments>${environments}</environments>
</configuration>
<executions>
<execution>
<id>dev_migrations</id>
<phase>validate</phase>
<goals>
<goal>migrate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<reportFormat>brief</reportFormat>
<trimStackTrace>true</trimStackTrace>
<useFile>false</useFile>
<includes>
<include>**/*Spec*.java</include>
<include>**/*Test*.java</include>
</includes>
<excludes>
<exclude>**/helpers/*</exclude>
<exclude>**/*$*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.github.jsimone</groupId>
<artifactId>webapp-runner</artifactId>
<version>7.0.22</version>
<destFileName>webapp-runner.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.javalite</groupId>
<artifactId>activejdbc</artifactId>
<version>2.0</version>
<exclusions>
<exclusion>
<groupId>opensymphony</groupId>
<artifactId>oscache</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.8.0-beta1</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>1.8.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.8</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>com.sun.xml.stream.buffer</groupId>
<artifactId>streambuffer</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.7</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>policy</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.gmbal</groupId>
<artifactId>gmbal-api-only</artifactId>
<version>3.2.0-b003</version>
</dependency>
<dependency>
<groupId>org.glassfish.ha</groupId>
<artifactId>ha-api</artifactId>
<version>3.1.9</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>snapshots1</id>
<name>JavaLite Snapshots1</name>
<url>http://repo.javalite.io/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>snapshots2</id>
<name>JavaLite Snapshots2</name>
<url>http://repo.javalite.io/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
答案 0 :(得分:1)
尝试更新的PostgreSQL驱动程序。这是Maven的定义:
SELECT winner
FROM nobel_win
WHERE (SUBJECT='Physics' AND Year=1970 )
OR (SUBJECT='Economics' AND Year=1971);
| winner |
|---------------|
| Hannes Alfven |
| Louis Neel |
| Simon Kuznets |