Import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.resultsetmetadata;public class Mysqltest {/** * @param args */public static void main ( String[] args) throws Exception {class.forname ("Com.mysql.jdbc.Driver");//string url= "jdbc:mysql://localhost:3306/ mydb?user=******&password=******&useoldaliasmetadatabehavior=true&useunicode=true& Characterencoding=utf-8&autoreconnect=true&failoverreadonly=false "; String url= "jdbc:mysql://localhost:3306/mydb?user=******&password=******&useunicode=true&characterencoding=utf-8&autoreconnect=true&failoverreadonly=false "; Connection conn = drivermanager.getconnection (URL); PreparedStatement stmt = conn.preparestatement ("SELECT ID as myid,name as myname from Test"); ResultSet rs = Stmt.executequery (); ResultSetMetaData RSMD = Rs.getmetadata (); String C1 = rsmd.getcolumnname (1); string C2 = Rsmd.getcolumnname (2), while (Rs.next ()) {String id = rs.getstring (c1),//Report column name cannot find exception String name = rs.getstring (C2 ); System.out.println (id+ "|" +name);} SYSTEM.OUT.PRINTLN (conn); Rs.close (); Stmt.close (); Conn.close ();}}
Exception in thread "main" Java.sql.SQLException:Column ' id ' not found.at com.mysql.jdbc.SQLError.createSQLException ( sqlerror.java:998) at Com.mysql.jdbc.SQLError.createSQLException (sqlerror.java:937) at Com.mysql.jdbc.SQLError.createSQLException (sqlerror.java:926) at Com.mysql.jdbc.SQLError.createSQLException ( sqlerror.java:872) at Com.mysql.jdbc.ResultSetImpl.findColumn (resultsetimpl.java:1073) at Com.mysql.jdbc.ResultSetImpl.getString (resultsetimpl.java:5212) at Com.bill99.mysql.MysqlTest.main ( MYSQLTEST.JAVA:25)
Workaround:
Add the Useoldaliasmetadatabehavior=true parameter to the JDBC URL
MySQL report column name cannot find an error