编译错误“无法找到符号”在DriverManager.getConnection()上 [英] Compilation error "Cannot find symbol" on DriverManager.getConnection()

查看:486
本文介绍了编译错误“无法找到符号”在DriverManager.getConnection()上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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);


推荐答案

以下是程序的编译版本。

尝试找出差异。
主要内容是导入异常处理

Here is compiling version of your program .
Try to make out difference. main things are import, exception handling

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);
                }
            }

        }
    }
}

这篇关于编译错误“无法找到符号”在DriverManager.getConnection()上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆