【问题标题】:mysql Connection with javamysql与java的连接
【发布时间】:2014-03-29 18:25:15
【问题描述】:

我尝试在 Eclipse 中连接到 java 中的 MySQL 数据库,但运行程序时出现此错误:

URLDecoder:转义 (%) 模式中的非法十六进制字符 - 对于输入字符串:“pa”

这是我的代码:

public static void main(String[] args) {
    try {
        Connection con=null;
        Statement stm=null;
        ResultSet resultSet=null;
        String host = "localhost:3306";
        String db = "mysqlconn";
        String driver = "com.mysql.jdbc.Driver";
        String user = "newuser";
        String pass = "123456";

        Class.forName(driver).newInstance();
        //String result = java.net.URLDecoder.decode(, "UTF-8");
        con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/myslconn?user=root%password=123456");
        stm = ((java.sql.Connection) con).createStatement();
        String sorgu = "SELECT * FROM mysqlconn";
        resultSet = stm.executeQuery(sorgu);
        while(resultSet.next()) {
            System.out.println(resultSet.getString("id"));
            //System.out.println(resultSet.getString("marka")); 
        }
    }
    catch(Exception ex) {
        System.err.println("Hata ! ");
        System.err.println(ex.getMessage());
    }    
}

【问题讨论】:

    标签: java mysql


    【解决方案1】:

    用于分隔 JDBC 连接字符串中参数的正确分隔符是 &,而不是 %。您的连接字符串应如下所示:

    con = (Connection) DriverManager.getConnection
           ("jdbc:mysql://localhost:3306/myslconn?user=root&password=123456");
    

    【讨论】:

      【解决方案2】:

      检查下面的代码.. 除了 用户名和密码 部分检查下面的部分之外,您的代码看起来也正确

      MyTacTics - MySql-Java Connection

      public class MyConnection
      {    
        static Connection con;
        public static Connection getConnection()
        {
          try
          {            
            if(con==null)
            {
              Class.forName("com.mysql.jdbc.Driver");  
              String url = "jdbc:mysql://localhost:3306/Your_DB_Name?"+
                           "user=db_user&password=user_password";
              con= DriverManager.getConnection(url);
            }
          }
          catch (Exception ex)
          {
              ex.printStackTrace();
          }        
          return con;
        }
      
        public static void CloseConnection()
        {
          try
          {
             con.close();
             con = null;
          }
          catch (SQLException e)
          {
             e.printStackTrace();
          } 
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-03-27
        • 2019-09-01
        • 2016-02-12
        • 2017-09-28
        • 1970-01-01
        • 1970-01-01
        • 2016-11-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多