Ankush Kalra
Ankush Kalra

Reputation: 91

Can getString() method of ResultSet can be used for getting the value of a TEXT type column from a MySQL table?

I am trying to retrieve the value of a TEXT field from a table in a MySQL database. MySQL version is 5.6.21 & I am using mysql-connector-java-5.1.18-bin.jar

My file is given below

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!");
    }
}

This says that my column does not exist although I have tried this on another table, it works on VARCHAR columns but not on TEXT columns

This error shows up:

But the table has a column named description:

Upvotes: 1

Views: 3683

Answers (1)

janos
janos

Reputation: 124666

The problem is NOT about the column type being text. You can get the value of a TEXT type using getString. You can verify in the documentation.

The problem is in the query:

query = "Select b_name, description columns from brands";

"columns" there is a mistake. Written this way, the description column is in fact renamed to columns in your result set. If you did rs.getString("columns") you would get the value. But that's not what you want to do. You want to fix the query by dropping that word:

query = "Select b_name, description from brands";

Upvotes: 1

Related Questions