⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tablemetainfo.java

📁 nanjing university cs 的java课件。 对新手很有用。付课件中源码。
💻 JAVA
字号:
/*
 * Created on 2004-8-9
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.sanshang.jdbc;

/**
 * @author WTQ
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
import java.sql.*;

public class TableMetaInfo {

	public static void main(String args[]) throws ClassNotFoundException,
			SQLException {
		Connection con = null;
		PreparedStatement stmt;
		String query = "describe user";

		try {
			// step 1 load driver
			Class.forName(DBInfo.driver);

		} catch (java.lang.ClassNotFoundException e) {
			e.printStackTrace();
		}

		try {
			// step 2 get connection
			con = DriverManager.getConnection(DBInfo.dbURI, DBInfo.user,
					DBInfo.password);
			// step 3 create statement
			stmt = con.prepareStatement(query);
			// step 4 execute query
			ResultSet rs = stmt.executeQuery();
			// step 5 Process the results
			ResultSetMetaData meta = rs.getMetaData();
			for (int i = 0; i < meta.getColumnCount(); i++) {
				System.out.print(meta.getColumnName(i + 1) + "("
						+ meta.getColumnType(i + 1) + ")" + " ");
			}
			System.out.println();
			while (rs.next()) {
				for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
					System.out.print(rs.getString(i + 1) + " ");
				}
				System.out.println();
			}
			stmt.close();

		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (con != null) {
					// step 6 close connection
					con.close();
				}
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -