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
programmer
1271 gold badge3 silver badges14 bronze badges
2 Answers 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
Mureinik
316k54 gold badges404 silver badges407 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
Deepak Sharma
4,1901 gold badge17 silver badges31 bronze badges
Comments
default