postgres 上的错误转换连接

发布于 2024-12-25 08:31:53 字数 3232 浏览 1 评论 0原文

大家晚安,这是我的第一个问题。

我在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剪不断理还乱 2025-01-01 08:31:53

DriverManager 对象返回 java.sql.Connection 的实现。它不会返回 controller.connection 类的实例。您的 conn 变量应为 java.sql.Connection 类型。

请注意:

  • 按照惯例,Java 中的类总是以大写字母开头,
  • 命名您的类 connection,并使其包装类 Connection 的对象,这确实是真的。令人困惑。如此令人困惑,以至于您对自己的代码感到困惑。

The DriverManager object returns an implementation of java.sql.Connection. It won't return an instance of your controller.connection class. Your conn variable should be of type java.sql.Connection.

Note that:

  • classes in Java, by convention, always start with an upper-case letter
  • naming your class connection, and make it wrap an object of class Connection, is really really confusing. So confusing that you got confused by your own code.
呆° 2025-01-01 08:31:53

将 this: 更改

private connection conn;

为 this:

private Connection conn;

并将 this:

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,并且类名不应以小写字母开头,因此最好将类重命名为 ConnectionUtilConnectionManager

//
// 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 中,约定有些不同。

Change this:

private connection conn;

to this:

private Connection conn;

and this:

conn =  (connection) DriverManager.getConnection(url,user,pwd);

to this:

conn =  DriverManager.getConnection(url,user,pwd);

JDBC's "Connection" class starts with an uppercase letter and resides in the java.sql package (the fully qualified class name is java.sql.Connection). You are accidentally using the lowercase letter connection which is the name of the class you have written into which java.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 or ConnectionManager:

//
// 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);
      }
  }

}

Similarly, you should rename lk_Controller to something like LKController. Unless in ZK the conventions are somewhat different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文