MySQL report column name cannot find an error

Source: Internet
Author: User
Tags stmt

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.