mysql与java的连接

mysql与java的连接,java,mysql,Java,Mysql,我试图在eclipse中连接到java中的MySQL数据库,但在运行程序时出现以下错误: URLDecoder:转义(%)模式中的非法十六进制字符-用于输入字符串:“pa” 这是我的密码: public static void main(String[] args) { try { Connection con=null; Statement stm=null; ResultSet resultSet=null; Strin

我试图在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());
    }    
}

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

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

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

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