I have not tried your code, but it looks workable at a glance.
Design-wise, we usually want to separate database access from other logic.
And likely you will want to define a class Course to hold this data and your business logic that operates on that data. For example, you may choose to implement a toHtmlTableRow method on Course that generates the HTML source. (In a more complex or sophisticated environment, you might also want to move that HTML-generation functionality to another class.) Something like this:
class Course {
String id, name, prereq;
public Course ( String id , String name , String prereq ) {
this.id = id;
this.name = name;
this.prereq = prereq;
}
public CharSequence toHtmlTableRow () {
StringBuilder html = new StringBuilder();
html.append( "<tr>\n" );
html.append( "<td>" + this.id + "</td><td>" + this.name + "</td><td>" + this.prereq + "</td>\n" );
html.append( "</tr>\n" );
return html;
}
// Override `Object`.
@Override
public String toString () {
return "Course{ " +
"id='" + id + '\'' +
" | name='" + name + '\'' +
" | prereq='" + prereq + '\'' +
" }";
}
}
Here is a complete working example app. For the sake of this demo, I crammed it all into a single .java file. In real work, I would not.
This example uses the H2 Database Engine. This example makes an in-memory database that never gets written to storage, again because this is a mere example.
Note the use of the try-with-resource & AutoCloseable syntax found in newer versions of Java to simplify working with JDBC.
package com.basilbourque.example;
import java.sql.*;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class DbToText {
public static void main ( String[] args ) {
DbToText app = new DbToText();
app.doIt();
}
private void doIt () {
try {
Class.forName( "org.h2.Driver" );
} catch ( ClassNotFoundException e ) {
e.printStackTrace();
}
List< Course > courses = new ArrayList();
try (
Connection conn = DriverManager.getConnection( "jdbc:h2:mem:db_to_text" ) ;
Statement stmt = conn.createStatement() ;
) {
String sql = "CREATE TABLE course_ ( \n" +
" id_ VARCHAR NOT NULL PRIMARY KEY , \n" +
" name_ VARCHAR NOT NULL , \n" +
" prereq_ VARCHAR NOT NULL \n" +
");";
stmt.execute( sql );
// Insert row.
sql = "INSERT INTO course_ ( id_ , name_ , prereq_ ) VALUES ( ? , ? , ? ) ;";
try (
PreparedStatement preparedStatement = conn.prepareStatement( sql ) ;
) {
preparedStatement.setString( 1 , "C01" );
preparedStatement.setString( 2 , "Course 1" );
preparedStatement.setString( 3 , "None" );
preparedStatement.executeUpdate();
preparedStatement.setString( 1 , "C02" );
preparedStatement.setString( 2 , "Course 2" );
preparedStatement.setString( 3 , "C01" );
preparedStatement.executeUpdate();
preparedStatement.setString( 1 , "C03" );
preparedStatement.setString( 2 , "Course 3" );
preparedStatement.setString( 3 , "C02" );
preparedStatement.executeUpdate();
}
// Query all.
sql = "SELECT * FROM course_";
try ( ResultSet rs = stmt.executeQuery( sql ) ; ) {
while ( rs.next() ) {
//Retrieve by column name
String id = rs.getString( "id_" );
String name = rs.getString( "name_" );
String prereq = rs.getString( "prereq_" );
// Instantiate a `Course` object for this data.
Course c = new Course( id , name , prereq );
courses.add( c );
}
}
} catch ( SQLException e ) {
e.printStackTrace();
}
System.out.println( "List of courses: \n" + courses );
System.out.println( "Courses as HTML table rows: " );
for ( Course course : courses ) {
System.out.println( course.toHtmlTableRow() );
}
}
class Course {
String id, name, prereq;
public Course ( String id , String name , String prereq ) {
this.id = id;
this.name = name;
this.prereq = prereq;
}
public CharSequence toHtmlTableRow () {
StringBuilder html = new StringBuilder();
html.append( "<tr>\n" );
html.append( "<td>" + this.id + "</td><td>" + this.name + "</td><td>" + this.prereq + "</td>\n" );
html.append( "</tr>\n" );
return html;
}
// Override `Object`.
@Override
public String toString () {
return "Course{ " +
"id='" + id + '\'' +
" | name='" + name + '\'' +
" | prereq='" + prereq + '\'' +
" }";
}
}
}
When run.
List of courses:
[Course{ id='C01' | name='Course 1' | prereq='None' }, Course{ id='C02' | name='Course 2' | prereq='C01' }, Course{ id='C03' | name='Course 3' | prereq='C02' }]
Courses as HTML table rows:
<tr>
<td>C01</td><td>Course 1</td><td>None</td>
</tr>
<tr>
<td>C02</td><td>Course 2</td><td>C01</td>
</tr>
<tr>
<td>C03</td><td>Course 3</td><td>C02</td>
</tr>