2014-03-29 28 views
1

连接MySQL我试图连接到在Eclipse Java中的MySQL数据库,但是当我跑我的节目,我有这样的错误:用java

URLDecoder:非法十六进制字符转义(%)模式 - 对于输入字符串: “啪”

这里是我的代码:

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

难道你的意思是'&'? –

回答

2

在JDBC conncetion字符串参数分离正确的分隔符是&,而不是%。您的连接字符串应该是这样的:

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

它解决了错误 – programmer

0

检查下面的代码..你的代码看起来也除了用户名和密码部分正确检查下面一个

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

非常感谢它解决了问题 – programmer