我正在使用eclipse为项目实现一段代码。 我被要求做的一个功能是找出数据库表中是否有两个值(用h2创建)。 所有这些必须通过使用DAO类和在main和DAO类之间传递的Manager类来实现。 我的问题是,通过这样做,总会引发一个异常,告诉我没有找到“数据包列表”表。 但这不可能是因为:
这些是类:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DaoPacchetti {
public static boolean Ricerca(String citta, String periodo ) throws SQLException{
boolean verifica=false;
PreparedStatement stat=null;
ResultSet rs=null;
Connection con=null;
con= DBManager.getConnection();
stat=con.prepareStatement("SELECT * FROM pacchettivisiteturistiche "
+ " WHERE citta=? AND disponibilitaSottoscrizione=?");
stat.setString(1, citta);
stat.setString(2, periodo);
rs=stat.executeQuery();
if(rs.next()){
verifica=true;
}
//rs.close();
//stat.close();
con.close();
return verifica;
}
}
经理班级:
import dao.DaoPacchetti;
//import dao.DAOException;
import java.sql.SQLException;
public class GestorePacchetti {
private static GestorePacchetti istance;
public static GestorePacchetti getIstance() {
if (istance == null)
istance = new GestorePacchetti();
return istance;
}
public boolean RicercaPacchetto(String citta,String periodo){
boolean trovato=false;
try{
trovato=DaoPacchetti.Ricerca(citta, periodo);
return trovato;
}
catch(SQLException e){
System.err.println("errore nella ricerca del pacchetto");
e.printStackTrace();
}
return trovato;
}
}
主要课程:
import pacchetti.GestorePacchetti;
import java.util.Scanner;
public class Terminale {
public static void main(String[] args) {
boolean trovato;
String citta;
String codice_pacchetto;
String data;
String nPartecipantistringa;
int nPartecipanti;
GestorePacchetti g= GestorePacchetti.getIstance();
System.out.println("immetti la città e la data per la ricerca del pacchetto
delle visite turistiche");
Scanner input = new Scanner(System.in);
do{
System.out.println("immetti prima la citta");
citta = input.nextLine();
System.out.println("ora immetti la data");
data=input.nextLine();
trovato=g.RicercaPacchetto(citta, data);
if(trovato==false)
System.out.println("Reinserire i parametri per favore");
}while(trovato==false);
System.out.println("Trovato!!");
}
}
我还编写了包含整个函数的main函数,而不调用其他类(这里的表被识别):
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception{
boolean trovato;
String citta;
String codice_pacchetto;
String data;
String nPartecipantistringa;
int nPartecipanti;
GestoreVisiteTuristiche g= new GestoreVisiteTuristiche();
System.out.println("immetti la città e la data per la ricerca del
pacchetto delle visite turistiche");
Scanner input = new Scanner(System.in);
System.out.println("immetti prima la citta");
citta = input.nextLine();
System.out.println("ora immetti la data");
data=input.nextLine();
Connection con=null;
PreparedStatement stat=null;
ResultSet rs=null;
try{
con=DBManager.getConnection();
stat=con.prepareStatement("SELECT * FROM PacchettiVisiteTuristiche P
"
+ "WHERE ?=P.citta AND ?=P.disponibilitaSottoscrizione");
stat.setString(1, citta);
stat.setString(2, data);
rs=stat.executeQuery();
if(rs.next()){
System.out.println("trovato!!");
citta=rs.getString("citta");
data=rs.getString("disponibilitaSottoscrizione");
System.out.println(citta+" "+data);
}
else{
System.out.println("errore, nessuna corrispondenza");
}
rs.close();
stat.close();
}
catch(Exception e){System.out.println("errore");}
finally {
if (con != null) {
try {
con.close();
} catch (SQLException e){}
}
}
}
}
DBManager
上课:
import java.sql.Connection;
public class DBManager {
protected static Connection conn;
final protected static ConnectionFactory CONNECTION_FACTORY=new
H2ConnectionFactory();
public static Connection getConnection(){
if (conn == null) {
try {
conn = CONNECTION_FACTORY.createConnection();
} catch(Exception e) {
e.printStackTrace();
}
}
return conn;
}
}
ConnectionFactory
:
public interface ConnectionFactory {
java.sql.Connection createConnection() throws Exception;
}
import java.sql.DriverManager;
public class H2ConnectionFactory implements ConnectionFactory {
protected final static String DB_PATH= "./test";
protected final static String CONNECTION_STRING = "jdbc:h2:" + DB_PATH ;
@Override
public java.sql.Connection createConnection() throws Exception {
Class.forName("org.h2.Driver");
return DriverManager.getConnection(CONNECTION_STRING, "sa", "12345");
}
}
这是引发的例外:
org.h2.jdbc.JdbcSQLException: Table "PACCHETTIVISITETURISTICHE" not found;
SQL statement:
SELECT * FROM pacchettivisiteturistiche WHERE citta=? AND
disponibilitaSottoscrizione=? [42102-195]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.command.Parser.readTableOrView(Parser.java:5506)
at org.h2.command.Parser.readTableFilter(Parser.java:1260)
at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1940)
at org.h2.command.Parser.parseSelectSimple(Parser.java:2089)
at org.h2.command.Parser.parseSelectSub(Parser.java:1934)
at org.h2.command.Parser.parseSelectUnion(Parser.java:1749)
at org.h2.command.Parser.parseSelect(Parser.java:1737)
at org.h2.command.Parser.parsePrepared(Parser.java:448)
at org.h2.command.Parser.parse(Parser.java:320)
at org.h2.command.Parser.parse(Parser.java:292)
at org.h2.command.Parser.prepareCommand(Parser.java:257)
at org.h2.engine.Session.prepareLocal(Session.java:573)
at org.h2.engine.Session.prepareCommand(Session.java:514)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1204)
at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:73)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:288)
at dao.DaoPacchetti.Ricerca(DaoPacchetti.java:22)
at pacchetti.GestorePacchetti.RicercaPacchetto(GestorePacchetti.java:21)
at ui.Terminale.main(Terminale.java:30)
答案 0 :(得分:0)
好的,但我不确定错误是否在这里因为在函数工作的主类中我使用相同的DBManager类。 这是我的DBManager类:
import java.sql.Connection;
public class DBManager {
protected static Connection conn;
final protected static ConnectionFactory CONNECTION_FACTORY=new
H2ConnectionFactory();
public static Connection getConnection(){
if (conn == null) {
try {
conn = CONNECTION_FACTORY.createConnection();
} catch(Exception e) {
e.printStackTrace();
}
}
return conn;
}
}
这是ConnectorFactory的接口和类:
public interface ConnectionFactory {
java.sql.Connection createConnection() throws Exception;
}
import java.sql.DriverManager;
public class H2ConnectionFactory implements ConnectionFactory {
protected final static String DB_PATH= "./test";
protected final static String CONNECTION_STRING = "jdbc:h2:" + DB_PATH ;
@Override
public java.sql.Connection createConnection() throws Exception {
Class.forName("org.h2.Driver");
return DriverManager.getConnection(CONNECTION_STRING, "sa", "12345");
}
}
答案 1 :(得分:-1)
jdbc:h2:~/test","sa","password"
或 jdbc:h2:file:~/test;USER=sa;PASSWORD=password
尝试任何这些连接字符串。