当前位置:编程学习 > 网站相关 >>

ResultSetMetaData用法

 

import java.sql.*;


public class TestResultSetMetaData {
  public static void main(String[] args)
      throws SQLException, ClassNotFoundException {
    // Load the JDBC driver
    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("Driver loaded");


    // Establish a connection
    Connection connection = DriverManager.getConnection
      ("jdbc:mysql://localhost/test", "root", "root");
    System.out.println("Database connected");


    // Create a statement
    Statement statement = connection.createStatement();


    // Execute a statement
    ResultSet resultSet = statement.executeQuery
      ("select * from course");


    ResultSetMetaData rsMetaData = resultSet.getMetaData();
    for (int i = 1; i <= rsMetaData.getColumnCount(); i++)
      System.out.printf("%-12s\t", rsMetaData.getColumnName(i));
    System.out.println();


    // Iterate through the result and print the student names
    while (resultSet.next()) {
      for (int i = 1; i <= rsMetaData.getColumnCount(); i++)
        System.out.printf("%-12s\t", resultSet.getObject(i));
      System.out.println();
    }


    // Close the connection
    connection.close();
  }
}

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,