gpt4 book ai didi

java - DriverManager.getConnection() 上的编译错误 "Cannot find symbol"

转载 作者:行者123 更新时间:2023-11-29 08:16:52 25 4
gpt4 key购买 nike

    import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Jdbc {

public static void main(String args[]) {



FileInputStream in = null;
Connection con = null;
try {

Properties props = new Properties();
in = new FileInputStream("/external/configuration/dir/db.properties");
props.load(in);
in.close();
String driver = props.getProperty("jdbc.driver");
if (driver != null) {
Class.forName(driver);
}
String host = props.getProperty("jdbc.host");
String port = props.getProperty("jdbc.port");
String database = props.getProperty("jdbc.database");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
con = DriverManager.getConnection(host,port,database,username, password);
} catch (Exception ex) {
Logger.getLogger(Jdbc.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
Logger.getLogger(Jdbc.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (con != null) {
try {
con.close();
} catch (Exception ex) {
Logger.getLogger(Jdbc.class.getName()).log(Level.SEVERE, null, ex);
}
}

}
}
}

错误是:

1 个错误

C:\Users\Desktop>javac Jdbc.java
Jdbc.java:32: cannot find symbol
symbol : method getConnection(java.lang.String,java.lang.String,java.lang.Strin
g,java.lang.String,java.lang.String)
location: class java.sql.DriverManager
con = DriverManager.getConnection(host,port,database,username, passw
ord);

最佳答案

这是你程序的编译版本。
试着找出区别。主要是导入异常处理

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Jdbc {

public static void main(String args[]) {



FileInputStream in = null;
Connection con = null;
try {

Properties props = new Properties();
in = new FileInputStream("/external/configuration/dir/db.properties");
props.load(in);
in.close();
String driver = props.getProperty("jdbc.driver");
if (driver != null) {
Class.forName(driver);
}
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
con = DriverManager.getConnection(url, username, password);
} catch (Exception ex) {
Logger.getLogger(Jdbc.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
Logger.getLogger(Jdbc.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (con != null) {
try {
con.close();
} catch (Exception ex) {
Logger.getLogger(Jdbc.class.getName()).log(Level.SEVERE, null, ex);
}
}

}
}
}

关于java - DriverManager.getConnection() 上的编译错误 "Cannot find symbol",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4094365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com