How do I print table data from database to Java Console? [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn’t work, and the expected results. See also: Stack Overflow question checklist
I’m not good with Java I’ve only learned the basics and it’s still kind of fresh somehow. I want to know how does someone print a row from a table to the Java Console.
That system.out.println thing I know that doesn’t work of course, I need something to replace it to print out the rows that comes from the SQL Statement right above it.
2 Answers 2
Try this tutorial from Oracle. It is simple and straight forward.
It would be something like this
Thank you, thank you, I actually tried bing and google, I just well couldn’t find the right place, I used wrong keywords. Anyway thank you again.
System.out.println("-------- MySQL JDBC Connection Testing ------------"); Connection connection = null; try < connection = DriverManager .getConnection("jdbc:mysql://localhost:3306/testDb", "userName", "Password"); >catch (SQLException e) < for(Throwable ex : e) < System.err.println("Error occurred " + ex); >e.printStackTrace(); > if (connection != null) < System.out.println("Connected to database!"); >else < System.out.println("Failed to make connection!"); >try < Statement stmt = connection.createStatement(); String query = "select * from person ;"; //person is the table name ResultSet rs = stmt.executeQuery(query); while (rs.next()) < String name = rs.getObject(1).toString(); String gender = rs.getObject(2).toString(); System.out.println("Name of the person is " + name + " and his gender is " + gender); //Person table has name and gender column >> catch (SQLException e) < e.printStackTrace(); for(Throwable ex : e) < System.err.println("Error occurred " + ex); >System.out.println("Error in fetching data"); >
Note : I am using JDBC 4 that come with Java 7 and hence no need to explicitly provide driver. Compiler will take it automatically from the class path. You can download MySqlConnector.jar and put it in your classpath.If you are not using java 7 you will have to explicitly load the driver using Class.forName() . And of course I am using MySql. the driver will change with the database you use.
Get value from ResultSet Java
Try get ting a different column. It’s entirely possible that «1» really is the value in the first column.
I want to get all column and return it to Person type like : Person person = rs.getSomething().getFirstname();
getString(1) will get you the value of first column in your select query. If you are getting 1 then it means the first value of the first column. You may get the desired column value using column name in rs.getString() method.
I resolved it. The column is marked from 1 not 0 like I thought so with getString(1) it will show the user_id = 1. With getString(2) it show: Quang Huy. Anyway, thank you 🙂
1 Answer 1
you dont put more details of your problem but this is an example :
you have a person class with this fields you could to make Setter Getter by yourself
then in your ResultSet :
think your table have two column («userid» and «firstname») and first column is «userid»
PreparedStatement ps = null; Connection con = null; // LIMIT 1 because you have one Person Object to fill otherwise you must be have an Array of Person String SQL = "SELECT * from usertable LIMIT 1"; try < con = DBConnection.getConnection(); ps = con.prepareStatement(SQL); ResultSet rs = ps.executeQuery(); Person p = null; while (rs.next()) < p = new Person(); p.id = rs.getInt(1); // or p.id=rs.getInt("userid"); by name of column p.name = rs.getString(2); // or p.name=rs.getString("firstname"); by name of column >return p; > catch ( SQLException ex) < Logger.getLogger(YourClassName.class.getName()).log(Level.SEVERE, null, ex); return null; >finally < if (con != null) < try < con.close(); >catch (SQLException ex) < Logger.getLogger(YourClassName.class.getName()).log(Level.SEVERE, null, ex); >> if (ps != null) < try < ps.close(); >catch (SQLException ex) < Logger.getLogger(YourClassName.class.getName()).log(Level.SEVERE, null, ex); >> >
if your Result is more than one you must be change Person to Person[] or «ArrayList» Object
Print SQL query from PreparedStatement to console in Java
I had a similar but not identical issue; I’ll post my solution here in case it will be useful to others:
when I did System.out.println(stmt.toString()); I would get:
«HikariProxyPreparedStatement@172796470 wrapping com.mysql.jdbc.JDBC42PreparedStatement@268fac52: SELECT . . . (rest of query here)»
but after some experimentation I changed it to
System.out.println(stmt.unwrap(com.mysql.jdbc.JDBC42PreparedStatement.class).asSql());
and now I get just the query. The unwrap() method returns the underlying JDBC PreparedStatement.
In general, the toString() behaviour of a PreparedStatement is implementation-specific: Get query from java.sql.PreparedStatement
Looks like what you’ve got is actually part of HikariCP’s prepared statement cache, which wraps the JDBC driver’s own PreparedStatement. To get the string out of it you’d need to access the underlying PreparedStatement and hope you’re using one of the drivers that supports this. (I’m having trouble figuring out if this is possible, looks like this area of HikariCP has been substantially refactored: github)
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.27.43548
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.