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());
 } 
}
takendarkk
3,4578 gold badges28 silver badges38 bronze badges
asked Mar 29, 2014 at 18:25
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");
answered Mar 29, 2014 at 18:27
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();
 } 
 }
}
answered Mar 29, 2014 at 18:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.