This question already has answers here:
JAVA illegal start of type
(2个答案)
两年前关闭。
第一次问这个问题,如果我做错了,请原谅我。
我在java中有一个postgresql的连接,如下所示:
Maven xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>SQL_Database</groupId>
<artifactId>SQL_Database</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
    <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.1.1</version>
    </dependency>

 </dependencies>


</project>

3类:Connect.java:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;

    public class Connect {

        private Connection conn;

        public Connect(){
        }

        public void setConnection(String database, String user, String password){
            String format = String.format("jdbc:postgresql://localhost/%s", database);
            Properties props = new Properties();
            props.put("user", user);
            props.put("password", password);
            try {
                this.conn = DriverManager.getConnection(format, props);
                this.conn.setAutoCommit(true);
            } catch (SQLException e){
                System.out.println(e.getMessage());
            }
        }

        public Connection getConnection(){
            return this.conn;
         }

        public void closeConnection() {
            try {
                this.conn.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }
    }

java语言:
   import java.sql.Connection;
   import java.sql.ResultSet;
   import java.sql.SQLException;


   class Sql {

   private Connection conn;
   private ResultSet results;

   public Sql(Connection conn) {
       this.conn = conn;
   }

   public void update(String query) {
       try {
           this.conn.prepareStatement(query).executeUpdate();
       } catch (SQLException e) {
           System.out.println(e.getMessage());
       }
   }

   public void select(String query) {
       try {
           this.results = this.conn.prepareStatement(query).executeQuery();
           getResults();
       } catch (SQLException e) {
           System.out.println(e.getMessage());
       }
   }

和Main.java:
import java.sql.Connection;

public class Main {

public static void main(String args[]){
    Connect c = new Connect();
    c.setConnection("foo", "foo", "foo");
    Connection conn = c.getConnection();
    Sql sql = new Sql(conn);
    String query = <Some SQL statement>;
    sql.update(query);
    sql.select(<Some SQL statement>);
    c.closeConnection();
  }
}

这很好,我可以创建表,从表中选择内容等。
但是,我希望在另一个项目中具有此连接,因此我在该项目中重新创建了Connect和Sql类,并将Main.java中的语句添加到另一个项目的主类中。
我可以做Connect c = new Connect();这很有效,但是当我尝试做c.setConnection("foo", "foo", "foo");时,它不起作用,Java无法识别c对象或其他东西。
sql一样。
我不明白为什么它在一个项目中起作用,但如果我在另一个项目中做同样的事情,它就不起作用。
这是我项目中不起作用的代码:
public class Main extends Application {
    Connect c = new Connect();
    c.setConnection("foo", "foo", "foo");
    Connection conn = c.getConnection();
    Sql sql = new Sql(conn);
    -- Some other code that works perfectly without the above--
   }

另外,对不起密码炸弹。

最佳答案

因为你的代码不在一个方法中。
应该是这样的:-

public class Main extends Application {

    public void connect() { // <-- Method declaration
        Connect c = new Connect();
        c.setConnection("foo", "foo", "foo");
        Connection conn = c.getConnection();
        Sql sql = new Sql(conn);
        //-- Some other code that works perfectly without the above--
    }

}

10-08 06:36
查看更多