Java mysql连接

public List<String> getDBTable(String tableName) {
	List<String> columns = new ArrayList<String>();
	String driver = "com.mysql.jdbc.Driver";
	String url = "jdbc:mysql://127.0.0.1:3306/sshdb";
	try {
		Class.forName(driver);
		Connection conn = DriverManager.getConnection(url, "root", "");
		Statement stm = conn.createStatement();
		ResultSet rs = stm.executeQuery("show columns from " + tableName);
		while(rs.next()) {
			columns.add(rs.getString("Field"));
		}
		rs.close();
		stm.close();
		conn.close();
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return columns;
}

posted @ 2017-10-09 18:31  翠微  阅读(201)  评论(0编辑  收藏  举报