简体   繁体   English

Java与Eclipse的JDBC连接(当方法调用方法时)

[英]JDBC connection in Java with Eclipse ( when a method calling a method)

I'm trying to connect to my DB using JDBC. 我正在尝试使用JDBC连接到我的数据库。 I wanted to make a method for connection and another method for selecting data. 我想制作一种连接方法和另一种选择数据的方法。 I am getting a red line in Eclipse on the 'Connection con = connectDB();' 我在Eclipse的“连接con = connectDB();”上看到一条红线 part. 部分。 ( See also attached) Cany anyone give me advice? (另请参阅)有人可以给我建议吗?

public class DBJdbc {

 //Statement stmt = null;

// connecting to DB
public void connectDB() {            

    //Connection con = null;

    try { 
        Class.forName("com.mysql.cj.jdbc.Driver"); 

        Connection con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "***", "***");
    }
    catch(SQLException e) { 
        e.printStackTrace();
    }
    catch(ClassNotFoundException e) {
        e.printStackTrace();
    }


}   

// a method for selecting DB
public static  void select() { 

    //connectDB(); 

    String sql = "SELECT * from SAC_SUR";

        try(Connection con = connectDB();  // I'm getting a red line here) 
            PreparedStatement pstmt = con.prepareStatement(sql)){
            Statement stmt = con.createStatement(); 
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                System.out.println("Id = " + id + "name = " + name);
            } //while 
        } catch(SQLException e) {
            System.out.println(e.getMessage());
        }
}   

red line here!!! 红线在这里!

connectDB() method is of void type and not returning anything but when you are calling the method, you are assigning it to variable con. connectDB()方法为void类型,不返回任何内容,但在调用该方法时,会将其分配给变量con。 So you need to change the return type of connectDb to the Connection type. 因此,您需要将connectDb的返回类型更改为Connection类型。

public Connection connectDB() {            

    Connection con = null;

    try { 
        Class.forName("com.mysql.cj.jdbc.Driver"); 

         con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "***", "***");
    }
    catch(SQLException e) { 
        e.printStackTrace();
    }
    catch(ClassNotFoundException e) {
        e.printStackTrace();
    }

return con;
}  

You are trying to call non-static method into the static area, which is not allowed in Java. 您试图将非静态方法调用到静态区域,这在Java中是不允许的。 So I made this method static and returning the database connection. 因此,我将此方法设为静态并返回数据库连接。

Please update the below method into your code. 请在您的代码中更新以下方法。 It will resolve your problem. 它将解决您的问题。

public static Connection connectDB() { 
    Connection con = null; 
    try { 
        Class.forName("com.mysql.cj.jdbc.Driver"); 
        con = DriverManager.getConnection("jdbc:mysql://****/SAC?useSSL=false&serverTimezone=UTC", "", ""); 
    } catch(SQLException e) { 
        e.printStackTrace(); 
    } catch(ClassNotFoundException e) { 
        e.printStackTrace(); 
    } 
    return con; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM