【问题标题】:How to specify path to mySQL using localhost?如何使用 localhost 指定 mySQL 的路径?
【发布时间】:2013-03-26 04:47:33
【问题描述】:

我在指定路径时遇到问题(我在以下文件夹中有两个文件:cmets.frm 和 db.opt:C:\xampp\mysql\data\feedback)...我正在使用 XAMPP 和 mySQL。我不知道为什么我有一个错误?请看一下我的这部分代码:

public void readDataBase() throws Exception {
   try {
        // This will load the MySQL driver, each DB has its own driver
      Class.forName("com.mysql.jdbc.Driver");
      // Setup the connection with the DB
    connect = DriverManager
         .getConnection("jdbc:mysql://localhost//feedback"
              + "user=root&password=1234");

PS:我的 localhost 密码是 12345678

【问题讨论】:

  • 连接字符串将是错误的,因为您将user 连接到feedbackjdbc:mysql://localhost//feedbackuser=root&password=1234

标签: java mysql path xampp localhost


【解决方案1】:

试试

 connect = DriverManager
     .getConnection("jdbc:mysql://localhost/feedback?user=root&password=1234")

(你忘了“反馈”后面的问号)

【讨论】:

    【解决方案2】:

    试试这个希望对你有帮助!!!

    Class.forName("com.mysql.jdbc.Driver");
    connect = DriverManager.getConnection("jdbc:mysql://localhost/feedback?"+"user=root&password=1234");
    

    还可以查看以下 URL 错误:

    jdbc:mysql://localhost//feedback?
    
    // after localhost... check and try my code...
    

    .

    【讨论】:

      【解决方案3】:

      我认为你需要使用这个:

      DriverManager.getConnection("jdbc:mysql://localhost:3306/feedback", "root", "1234");
      

      我检查了我的旧项目,发现我这里没有使用双斜杠/feedback

      你也可以像这样指定编码:

       DriverManager.getConnection("jdbc:mysql://localhost:3306/feedback?characterEncoding=UTF-8&characterEncoding=Cp1251", "root", "1234");
      

      还有一个建议。不要使用硬代码。从属性文件中获取 url、密码和用户名。

      【讨论】:

        【解决方案4】:

        很多答案,但每个人都忘记了数据库的端口。

        如果您使用 mysql,请尝试 3306 作为 db 端口。
        http://www.petefreitag.com/articles/jdbc_urls/ - jdbc url 列表(示例)

        try 
        { 
            conn = DriverManager.getConnection("jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName, user, passwd); 
        } 
        catch(SQLException sqle) 
        { 
            System.out.println("Connection fails: " + sqle.getMessage()); 
        }
        

        【讨论】:

          【解决方案5】:

          为了更清楚,您可以在获取连接时使用其他重载方法。

          Class.forName("com.mysql.jdbc.Driver");
          conn = DriverManager.getConnection("jdbc:mysql://<db_ip>/<db_name>", 
              "<username>", "<pwd>");
          

          【讨论】:

            【解决方案6】:

            你应该试试

            DriverManager.getConnection("jdbc:mysql://localhost/feedback", "root", "1234");
            

            【讨论】:

              猜你喜欢
              • 2013-06-10
              • 2011-12-05
              • 2014-07-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多