package com.jini.jdbc;
import java.sql.*;
public class PrintColumns {
public static void main(String args[]) {
String url = "jdbc:mysql://localhost/test";
Connection con;
String query = "select * from STUDENT";
Statement stmt;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url, "root", "123");
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
PrintColumnTypes.printColTypes(rsmd);
System.out.println("");
int numberOfColumns = rsmd.getColumnCount();
for (int i = 1; i <= numberOfColumns; i++) {
if (i > 1)
System.out.print(", ");
String columnName = rsmd.getColumnName(i);
System.out.print(columnName);
}
System.out.println("");
while (rs.next()) {
for (int i = 1; i <= numberOfColumns; i++) {
if (i > 1)
System.out.print(", ");
String columnValue = rs.getString(i);
System.out.print(columnValue);
}
System.out.println("");
}
stmt.close();
con.close();
} catch (SQLException ex) {
System.err.print("SQLException: ");
System.err.println(ex.getMessage());
}
}
} // end of class
class PrintColumnTypes {
public static void printColTypes(ResultSetMetaData rsmd)
throws SQLException {
int columns = rsmd.getColumnCount();
for (int i = 1; i <= columns; i++) {
int jdbcType = rsmd.getColumnType(i);
String name = rsmd.getColumnTypeName(i);
System.out.print("Column " + i + " is JDBC type " + jdbcType);
System.out.println(", which the DBMS calls " + name);
}
}
} // end of class
import java.sql.*;
public class PrintColumns {
public static void main(String args[]) {
String url = "jdbc:mysql://localhost/test";
Connection con;
String query = "select * from STUDENT";
Statement stmt;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url, "root", "123");
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
PrintColumnTypes.printColTypes(rsmd);
System.out.println("");
int numberOfColumns = rsmd.getColumnCount();
for (int i = 1; i <= numberOfColumns; i++) {
if (i > 1)
System.out.print(", ");
String columnName = rsmd.getColumnName(i);
System.out.print(columnName);
}
System.out.println("");
while (rs.next()) {
for (int i = 1; i <= numberOfColumns; i++) {
if (i > 1)
System.out.print(", ");
String columnValue = rs.getString(i);
System.out.print(columnValue);
}
System.out.println("");
}
stmt.close();
con.close();
} catch (SQLException ex) {
System.err.print("SQLException: ");
System.err.println(ex.getMessage());
}
}
} // end of class
class PrintColumnTypes {
public static void printColTypes(ResultSetMetaData rsmd)
throws SQLException {
int columns = rsmd.getColumnCount();
for (int i = 1; i <= columns; i++) {
int jdbcType = rsmd.getColumnType(i);
String name = rsmd.getColumnTypeName(i);
System.out.print("Column " + i + " is JDBC type " + jdbcType);
System.out.println(", which the DBMS calls " + name);
}
}
} // end of class
No comments:
Post a Comment