postgres 上的错误转换连接
大家晚安,这是我的第一个问题。
我在使用tomcat web服务器运行ZK应用程序时遇到一些问题
java.lang.ClassCastException: org.postgresql.jdbc4.Jdbc4Connection 无法转换为controller.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 = (连接) DriverManager.getConnection(url,用户,密码);
我无法将投射连接更改为: conn = DriverManager.getConnection(url,用户,密码);
请有人帮忙,我被困了一夜 TT
Good night all, this is my first question.
i got some problem when running ZK application with tomcat webserver
java.lang.ClassCastException: org.postgresql.jdbc4.Jdbc4Connection cannot be cast to 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)
this is my connection class:
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);
}
}
}
and this is my controller class
//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 said error at line:
DriverManager.registerDriver(new org.postgresql.Driver());
conn = (connection) DriverManager.getConnection(url,user,pwd);
i cant changed the cast connection to:
conn = DriverManager.getConnection(url,user,pwd);
please somebody help, i got stuck over night T.T
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DriverManager 对象返回 java.sql.Connection 的实现。它不会返回
controller.connection
类的实例。您的 conn 变量应为 java.sql.Connection 类型。请注意:
connection
,并使其包装类Connection
的对象,这确实是真的。令人困惑。如此令人困惑,以至于您对自己的代码感到困惑。The DriverManager object returns an implementation of
java.sql.Connection
. It won't return an instance of yourcontroller.connection
class. Yourconn
variable should be of typejava.sql.Connection
.Note that:
connection
, and make it wrap an object of classConnection
, is really really confusing. So confusing that you got confused by your own code.将 this: 更改
为 this:
并将 this:
更改为这样:
JDBC 的
"Connection"
类以大写字母开头,驻留在java.sql
包中(完全限定的类名是java.sql.Connection)。您不小心使用了小写字母connection
,它是您写入的类的名称,java.sql.Connection
无法进行强制转换。另外,在 Java 中,类名的约定是使用 CamelCase,并且类名不应以小写字母开头,因此最好将类重命名为
ConnectionUtil
或ConnectionManager
:同样,您应该将
lk_Controller
重命名为LKController
之类的名称。除非在 ZK 中,约定有些不同。Change this:
to this:
and this:
to this:
JDBC's
"Connection"
class starts with an uppercase letter and resides in thejava.sql
package (the fully qualified class name isjava.sql.Connection
). You are accidentally using the lowercase letterconnection
which is the name of the class you have written into whichjava.sql.Connection
cannot be cast.Also, in Java, the convention is to use CamelCase for class names and class names should not start with lower case, so it's better to rename your class to something like
ConnectionUtil
orConnectionManager
:Similarly, you should rename
lk_Controller
to something likeLKController
. Unless in ZK the conventions are somewhat different.