Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ResultSet的getString()方法能否用于从MySQL表中获取文本类型列的值?_Java_Mysql_Jdbc - Fatal编程技术网

Java ResultSet的getString()方法能否用于从MySQL表中获取文本类型列的值?

Java ResultSet的getString()方法能否用于从MySQL表中获取文本类型列的值?,java,mysql,jdbc,Java,Mysql,Jdbc,我试图从MySQL数据库中的表中检索文本字段的值。 MySQL版本是5.6.21 &我使用的是mysql-connector-java-5.1.18-bin.jar 我的档案如下 import java.sql.*; public class DatabaseConnection { static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mys

我试图从MySQL数据库中的表中检索文本字段的值。 MySQL版本是5.6.21 &我使用的是mysql-connector-java-5.1.18-bin.jar

我的档案如下

import java.sql.*;

public class DatabaseConnection {

    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost/book";

    // database credentials
    static final String USER = "username";
    static final String PASS = "password";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {

            Class.forName("com.mysql.jdbc.Driver");

            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            System.out.println("Creating statement...");
            stmt = conn.createStatement();
            String query;
            query = "Select b_name, description columns from brands";
            ResultSet rs = stmt.executeQuery(query);

            while(rs.next()) {

                String first_name = rs.getString("b_name");
                String description = rs.getString("description");

                System.out.println(first_name);
                System.out.println(description);
            }

            rs.close();
            stmt.close();
            conn.close();
        } catch(SQLException se) {
            se.printStackTrace();
        } catch(ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        }
        finally{
            //finally block used to close resources
            try{
                if(stmt!=null)
                    stmt.close();
            } catch(SQLException se2){
            }// nothing we can do
            try{
                if(conn!=null)
                    conn.close();
            } catch(SQLException se){
                se.printStackTrace();
            }//end finally try
        }//end try
        System.out.println("Goodbye!");
    }
}
这意味着我的列不存在,尽管我在另一个表上尝试过,但它在VARCHAR列上有效,但在文本列上无效

此错误显示为:


但是该表有一个名为description的列:

问题不在于列类型是文本。 您可以使用
getString
获取文本类型的值。 您可以在中进行验证

问题出在查询中:

“列”有一个错误。 通过这种方式编写,
description
列实际上被重命名为结果集中的
columns
。 如果您执行了
rs.getString(“columns”)
,您将获得该值。 但那不是你想做的。要通过删除该单词来修复查询:

query = "Select b_name, description from brands";

请注意,MySQL Connector/J5.1.18非常旧(6年),最新版本是5.1.42。您可能需要考虑升级,请参阅
query = "Select b_name, description from brands";