晚安,这是我的第一个问题。
使用tomcat webserver运行ZK应用程序时遇到了一些问题
java.lang.ClassCastException:org.postgresql.jdbc4.Jdbc4Connection无法强制转换为controller.connection controller.connection。(connection.java:27) controller.lap_keuangan.lk_Controller.preparelistlk(lk_Controller.java:34) controller.lap_keuangan.lk_Controller.doAfterCompose(lk_Controller.java:30)
这是我的连接类:
package controller;
//import ommited
public class connection {
private static final String url="jdbc:postgresql://192.168.56.101:5432/adempiere";
private static final String user = "postgres";
private static final String pwd = "auliaardy";
private connection conn;
public connection()throws Exception{
try {
DriverManager.registerDriver(new org.postgresql.Driver());
conn = (connection) DriverManager.getConnection(url,user,pwd);
} catch (SQLException e) {
System.out.println("Driver tidak ditemukan"+e);
}
}
public Connection getConnection(){
return (Connection) conn;
}
public void closeConnection(){
try {
conn.closeConnection();
} catch (Exception e) {
System.out.println("gagal menutup koneksi disebabkan: "+e);
}
}
}
,这是我的控制器类
//import ommited here
public class lk_Controller extends GenericForwardComposer {
private Listbox listlk;
@Override
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
preparelistlk();
}
public void preparelistlk() throws Exception {
connection c = new connection();
try {
l_keuangan lk = new l_keuangan(c.getConnection());
List<tbl_lk> llk = lk.getLKeuangan();
ListModelList lml = new ListModelList(llk, true);
listlk.setModel(lml);
listlk.setItemRenderer(new ListitemRenderer() {
@Override
public void render(Listitem lslk, Object o) throws Exception {
try {
tbl_lk tl = (tbl_lk) o;
new Listcell(ubahtanggal()).setParent(lslk);
new Listcell(Integer.toString(tl.getSum_debit())).setParent(lslk);
new Listcell(Integer.toString(tl.getSum_credit())).setParent(lslk);
new Listcell(Integer.toString(tl.getTotal())).setParent(lslk);
} catch (Exception e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public String ubahtanggal() {
tbl_lk lk = new tbl_lk();
DateFormat df = new SimpleDateFormat("YYYY/MM/DD");
long tanggal = lk.getTanggal().getTime();
String tgl = df.format(tanggal);
return tgl;
}
}
tomcat说错误在线: DriverManager.registerDriver(new org.postgresql.Driver()); conn =(connection)DriverManager.getConnection(url,user,pwd);
我无法将演员表连接更改为: conn = DriverManager.getConnection(url,user,pwd);
请有人帮忙,我被困了一夜T.T
答案 0 :(得分:2)
DriverManager对象返回java.sql.Connection
的实现。它不会返回您的controller.connection
类的实例。您的conn
变量应为java.sql.Connection
类型。
请注意:
connection
,并让它包装类Connection
的对象,真的很混乱。很困惑,你被自己的代码搞糊涂了。答案 1 :(得分:0)
改变这个:
private connection conn;
到此:
private Connection conn;
和此:
conn = (connection) DriverManager.getConnection(url,user,pwd);
到此:
conn = DriverManager.getConnection(url,user,pwd);
JDBC的"Connection"
类以大写字母开头,位于java.sql
包中(完全限定的类名为java.sql.Connection
)。您不小心使用了小写字母connection
,这是您编写的类的名称,java.sql.Connection
无法转换。
另外,在Java中,惯例是将CamelCase用于类名,类名不应以小写字母开头,因此最好将类重命名为ConnectionUtil
或ConnectionManager
:< / p>
//
// NOTE: must be in ConnectionManager.java
//
package controller;
// imports ommited
public class ConnectionManager {
private static final String url="jdbc:postgresql://192.168.56.101:5432/adempiere";
private static final String user = "postgres";
private static final String pwd = "auliaardy";
private Connection conn;
public Connection() throws Exception{
try {
DriverManager.registerDriver(new org.postgresql.Driver());
conn = (Connection) DriverManager.getConnection(url,user,pwd);
} catch (SQLException e) {
System.out.println("Driver tidak ditemukan"+e);
}
}
public Connection getConnection() {
return conn;
}
public void closeConnection() {
try {
conn.closeConnection();
} catch (Exception e) {
System.out.println("gagal menutup koneksi disebabkan: "+e);
}
}
}
同样,您应该将lk_Controller
重命名为LKController
。除非在ZK中,惯例有些不同。