package com.mysql;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class CheckTableAndColumnExist {

	public static void main(String[] args) {
		Connection connection = null;
		try {
			String userName = "root";
			String password = "111111";
			String jdbcurl = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			connection = DriverManager.getConnection(jdbcurl, userName,
					password);
			DatabaseMetaData md = connection.getMetaData();
			String tableName = "staff";
			String columnName = "column_name";
			ResultSet rs = md.getTables(null, null, tableName, null);
			if (rs.next()) {
				System.out.println("Table " + tableName + " exist");
				rs = md.getColumns(null, null, tableName, columnName);
				if (rs.next()) {
					System.out.println("Table " + tableName
							+ " exist in Table " + tableName);
				} else {
					System.out.println("Column " + tableName
							+ " not exist in Table " + tableName);
				}
			} else {
				System.out.println("Table " + tableName + " not exist");
			}
		} catch (Exception e) {
			System.err.println("Cannot connect to database server,Exception:"
					+ e.getMessage());
		} finally {
			if (connection != null) {
				try {
					connection.close();
					connection = null;
				} catch (Exception e) { /* ignore close errors */
				}
			}
		}
	}
}
最近下载更多
MIAOHUA  LV6 2021年1月31日
ncd12320  LV8 2020年1月12日
luohaipeng  LV23 2019年11月19日
xiaoxiao303  LV8 2019年3月5日
越前龍马  LV9 2016年10月21日
cxming  LV6 2016年9月8日
Happy犇爺  LV3 2016年2月2日
xujinlong  LV10 2015年11月22日
ewf_momo  LV16 2015年9月16日
821255903  LV1 2015年9月8日
最近浏览更多
zt3631877  LV9 2023年10月20日
brownwang  LV1 2023年10月14日
13765325770 2022年11月3日
暂无贡献等级
akbar2020  LV9 2022年8月27日
沐兮20010401  LV7 2021年6月18日
lodddy  LV6 2021年5月9日
book123  LV4 2021年3月4日
a844534272  LV1 2021年2月14日
MIAOHUA  LV6 2021年1月29日
18238687719  LV1 2021年1月10日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友