jdbc中的postgres驱动程序出错

时间:2016-08-12 19:25:49

标签: java eclipse postgresql

我尝试使用JDBC为postgres创建驱动程序,但我在驱动程序管理器中收到错误。我该如何修复此代码?

import java.sql.*;

public class jdbc {
    public static void main(String[] args) {
        try {
            Class.forName("org.postgresql.Driver");
            Connection con=DriverManager.getConnection("jdbc:postgresql://localhost:5432/dvdrental2","postgres","vibhug");
            System.out.println("Opened database successfully");
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery("select * from actor ");

            while(rs.next())  
            {
                int actor_id = rs.getInt("actor_id");
                String  first_name = rs.getString("first_name");
                int last_name  = rs.getInt("last_name");

                System.out.println( "ID = " + actor_id );
                System.out.println( "fNAME = " + first_name );
                System.out.println( "AGE = " + last_name );

                System.out.println();
            }
            con.close();  
        } catch (ClassNotFoundException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("operation done successfully!!!");
    }
}

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试检索last_name,这很可能是int的字符串。修复它,它应该没问题:

String last_name  = rs.getString("last_name");