我如何从ini [odbc]中检索以连接我的应用

时间:2019-02-12 10:53:29

标签: mysql javafx javafx-8 javafx-2 teamsql

我遇到一些问题如何将config.ini ---> [odbc]数据库连接检索到我的getconnection类中?我正在使用mysql数据库 我将连接存储到数据库中,并且Config.ini-[odbc]很好 但是当我想检索数据库并将其连接到应用程序trow config.ini中时,我无法

检查这是我的数据库类:

public class DB(
public static String url = "jdbc:mysql://localhost/resturno?useSSL=false&autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8";
    public static String user = "root";
    public static String paw = "";

    public static Connection getConnection() {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            return DriverManager.getConnection(url, user, paw);
        } catch (ClassNotFoundException | SQLException ex) {
            System.out.println(ex.getMessage());
            System.out.println("couldn't connect!");
            throw new RuntimeException(ex);
        }
    }
)

这是我的Config.ini:

[ODBC]
ServerName = jdbc:mysql://localhost
DataBase = resturno?useSSL=false&autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8
Username = root
Password = 
Year = 2019

这是我的readIni类:

public void readIni() {

        try {
            File file = new File(pathIni);
            if (file.exists()) {
                Wini wini = new Wini(new File(pathIni));
                String url = wini.get("ODBC", "ServerName");
                String dbnm = wini.get("ODBC", "DataBase");
                String dbus = wini.get("ODBC", "Username");
                String dbpas = wini.get("ODBC", "Password");
                String dbyer = wini.get("ODBC", "Year");
                if ((url != null && !url.equals("")) && (dbnm != null && !dbnm.equals("")) && (dbus != null && !dbus.equals("")) && (dbpas != null && !dbpas.equals("")) && (dbyer != null && !dbyer.equals(""))) {
                    Serverurl.setText(url);
                    dbname.setText(dbnm);
                    dbuser.setText(dbus);
                    dbpass.setText(dbpas);
                    dbyear.setText(dbyer);

                }
            }
        } catch (IOException ex) {
            Logger.getLogger(DB.class.getName()).log(Level.SEVERE, null, ex);

        }
    }

如果您有任何遗漏,我会帮助我处理我的代码

1 个答案:

答案 0 :(得分:0)

您可能缺少数据库的端口。

public static String url = "jdbc:mysql://localhost:<PORT>/resturno?useSSL=false&autoReconnect=true&useUnicode=yes&characterEncoding=UTF-8";

通常端口是3306。希望对您有所帮助。