我有一个名为“Duuni”的对象,里面有一些抽象的getter和setter方法,以及Duuni的“DuuniImpl”实现。我使用JdbcTemplate和RowMapper来从数据库中检索数据。这是我的代码:
package vjb.de.vietjob.bean;
import java.util.Date;
public interface Duuni {
public abstract int getId();
public abstract void setId(int id);
public abstract String getNimi();
public abstract void setNimi(String nimi);
public abstract Date getPaiva();
public abstract void setPaiva(Date paiva);
public abstract String getTiedot();
public abstract void setTiedot(String tiedot);
public abstract String getPaikka();
public abstract void setPaikka(String paikka);
public abstract String getPalkka();
public abstract void setPalkka(String palkka);
}
和DuuniImpl课程:
package vjb.de.vietjob.bean;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DuuniImpl implements Duuni {
SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd");
private int id;
private String nimi;
private Date paiva;
private String tiedot;
private String paikka;
private String palkka;
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getNimi() {
return this.nimi;
}
public void setNimi(String nimi) {
this.nimi = nimi;
}
public Date getPaiva() {
return paiva;
}
public void setPaiva(Date paiva) {
this.paiva = paiva;
}
public String getTiedot() {
return this.tiedot;
}
public void setTiedot(String tiedot) {
this.tiedot = tiedot;
}
public String getPaikka() {
return this.paikka;
}
public void setPaikka(String paikka) {
this.paikka = paikka;
}
public String getPalkka() {
return this.palkka;
}
public void setPalkka(String palkka) {
this.palkka = palkka;
}
@Override
public String toString() {
return "duuniIplm [id=" + id + ", nimi=" + nimi + ", paiva=" + paiva
+ ", tiedot=" + tiedot + ", paikka=" + paikka + ", palkka="
+ palkka + "]";
}
}
DuuniDaoImpl类:
package vjb.de.vietjob.dao;
import java.util.List;
import javax.inject.Inject;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import vjb.de.vietjob.bean.Duuni;
@Repository
public class DuuniDaoImpl implements DuuniDao {
@Inject
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate(){
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate=jdbcTemplate;
}
public List<Duuni> showDuuni() {
String sql = "select * from duuni";
RowMapper<Duuni> duuniRM = new DuuniRowMapper();
List<Duuni> list = jdbcTemplate.query(sql, duuniRM);
return list;
}
public void postDuuni(Duuni duuni) {
// TODO Auto-generated method stub
}
public void deleteDuuni(int id) {
// TODO Auto-generated method stub
}
public void postDuuni() {
// TODO Auto-generated method stub
}
}
DuuniRowMapper类:
package vjb.de.vietjob.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import vjb.de.vietjob.bean.*;
public class DuuniRowMapper implements RowMapper<Duuni> {
public Duuni mapRow(ResultSet rs, int row) throws SQLException {
Duuni duuni = new DuuniImpl();
duuni.setId(rs.getInt("id"));
duuni.setNimi(rs.getString("nimi"));
duuni.setPaikka(rs.getString("paikka"));
duuni.setPaiva(rs.getDate("paiva"));
duuni.setPalkka(rs.getString("palkka"));
duuni.setTiedot(rs.getNString("tiedot"));
return duuni;
}
}
最后是处理来自数据库的数据的应用程序:
package vjb.de.vietjob.dao;
import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import vjb.de.vietjob.bean.Duuni;
public class test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("vietjob-data.xml");
DuuniDao dao = (DuuniDao) context.getBean("duuniDao");
List<Duuni> d = dao.showDuuni();
for(Duuni duuni : d){
System.out.println(duuni.getNimi());
}
context.close();
}
}
但是当我运行它时,错误来自“NString not supportedted”:
0 [main] INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@504bae78: startup date [Mon Mar 07 17:25:45 EET 2016]; root of context hierarchy
60 [main] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [vietjob-data.xml]
385 [main] INFO org.springframework.context.support.PropertySourcesPlaceholderConfigurer - Loading properties file from URL [file:/Volumes/Data/JavaEE/workspace/vietjob/target/classes/db_connection.properties]
635 [main] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
719 [main] INFO org.springframework.jdbc.support.SQLErrorCodesFactory - SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]
Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: StatementCallback; SQL [select * from duuni]; NString not supported; nested exception is java.sql.SQLFeatureNotSupportedException: NString not supported
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:94)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:416)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:471)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:481)
at vjb.de.vietjob.dao.DuuniDaoImpl.showDuuni(DuuniDaoImpl.java:35)
at vjb.de.vietjob.dao.test.main(test.java:14)
Caused by: java.sql.SQLFeatureNotSupportedException: NString not supported
at org.mariadb.jdbc.internal.SQLExceptionMapper.getFeatureNotSupportedException(SQLExceptionMapper.java:165)
at org.mariadb.jdbc.MySQLResultSet.getNString(MySQLResultSet.java:2679)
at org.apache.commons.dbcp.DelegatingResultSet.getNString(DelegatingResultSet.java:802)
at org.apache.commons.dbcp.DelegatingResultSet.getNString(DelegatingResultSet.java:802)
at vjb.de.vietjob.dao.DuuniRowMapper.mapRow(DuuniRowMapper.java:17)
at vjb.de.vietjob.dao.DuuniRowMapper.mapRow(DuuniRowMapper.java:1)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:93)
at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:60)
at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:460)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:405)
... 4 more
我正在使用MariaDB。
答案 0 :(得分:1)
使用rs.getString("tiedot")
代替rs.getNString("tiedot")
,因为MariaDB Connector / J不支持它。
答案 1 :(得分:0)
尝试使用最新版本的MariaDB客户端。
根据this jira,此问题已解决。