Skip to content

Instantly share code, notes, and snippets.

@uncoded-ro
Created January 12, 2020 06:41
Show Gist options
  • Save uncoded-ro/cb22467bd0943850eb914f557d0cf7ea to your computer and use it in GitHub Desktop.
Save uncoded-ro/cb22467bd0943850eb914f557d0cf7ea to your computer and use it in GitHub Desktop.
package ro.virtualcampus.db;
import java.sql.*;
public class Actors {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ResultSetMetaData meta = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://data.virtualcampus.ro:3306/sakila",
"airman", "parola");
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM actor WHERE last_name LIKE 'B%' LIMIT 10;");
meta = rs.getMetaData();
for (int i = 0; i < meta.getColumnCount(); i++)
System.out.print(meta.getColumnName(i + 1) + "\t");
System.out.print("\n");
while (rs.next()) {
for (int i = 0; i < meta.getColumnCount(); i++)
System.out.print(rs.getObject(i + 1) + "\t");
System.out.print("\n");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment