试图为postgresql使用jdbc驱动程序,但它不起作用

时间:2010-10-30 21:58:36

标签: java postgresql jdbc osx-snow-leopard

据我所知,我已将jdbc驱动程序添加到我的类路径中,即我将以下内容添加到我的.profile

export CLASSPATH = $ CLASSPATH:location / to / the / jarfile.jar

当我编译我的java程序时,我总是得到这个错误

javac v9.java
v9.java:8: <identifier> expected
 Class.forName("org.postgresql.Driver");//load the driver
          ^
v9.java:8: illegal start of type
 Class.forName("org.postgresql.Driver");//load the driver
           ^
2 errors

这让我疯了,任何帮助都会很棒。我正在使用Mac OS X Snow Leopard

java程序在这里

import java.sql.*;

public class v9
{
 String dbURL = "jdbc:postgresql:mydb";
 String user = "UserName";
 String password = "pswd";
 C  try
{
Class.forName("org.postgresql.Driver");//load the driver
// Connect to the database
Connection DBconn = DriverManager.getConnection( dbURL, user, password );
}
catch (Exception e)
{
    e.printStackTrace();
}
}

1 个答案:

答案 0 :(得分:3)

试试这个 - 你需要一个方法:

import java.sql.Connection;
import java.sql.DriverManager;

public class V9
{
    public static final String driver = "org.postgresql.Driver";
    public static final String url = "jdbc:postgresql://localhost:5432/party";
    public static final String username = "pgsuper";
    public static final String password = "pgsuper";

    public static void main(String [] args)
    {
        try
        {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, username, password);
            System.out.println(conn.getMetaData().getDatabaseProductName());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
相关问题