Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql Connection with java

Tags:

java

mysql

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());
    }    
}
like image 537
programmer Avatar asked Apr 21 '26 04:04

programmer


1 Answers

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");
like image 59
Mureinik Avatar answered Apr 23 '26 18:04

Mureinik