3

I tried to connect to a MySQL database in java in eclipse but I have this error when I run my program:

URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "pa"

Here is my code:

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

2 Answers 2

2

The correct delimiter for separating the parameters in the JDBC conncetion string is &, not %. Your connection string should look like this:

con = (Connection) DriverManager.getConnection
       ("jdbc:mysql://localhost:3306/myslconn?user=root&password=123456");
Sign up to request clarification or add additional context in comments.

Comments

0

Check the below code.. you code also looks correct except the username and password part check the below one

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.