mysql获取表名并查询表
一.单表查询1.查询所有数据(select * FROM tb_name;)select 后面跟上要查询的字段,* 号代表所有的字段,一般来说,查询所有字段是最耗时长的,所以今后查询数据尽可能按需索取...
2024.11.26原生jdbc方式:
Statement.getGeneratedKeys()
示例:
Statement stmt = null;ResultSet rs = null;try { // // Create a Statement instance that we can use for // ‘normal‘ result sets assuming you have a // Connection ‘conn‘ to a MySQL database already // available stmt = conn.createStatement(); // // Issue the DDL queries for the table for this example // stmt.executeUpdate("drop TABLE IF EXISTS autoIncTutorial"); stmt.executeUpdate( "CREATE TABLE autoIncTutorial (" + "priKey INT NOT NULL AUTO_INCREMENT, " + "dataField VARCHAR(64), PRIMARY KEY (priKey))"); // // insert one row that will generate an AUTO INCREMENT // key in the ‘priKey‘ field // stmt.executeUpdate( "insert INTO autoIncTutorial (dataField) " + "values (‘Can I Get the Auto Increment Field?‘)", Statement.RETURN_GENERATED_KEYS); // // Example of using Statement.getGeneratedKeys() // to retrieve the value of an auto-increment // value // int autoIncKeyFromApi = -1; rs = stmt.getGeneratedKeys(); if (rs.next()) { autoIncKeyFromApi = rs.getInt(1); } else { // throw an exception from here } System.out.println("Key returned from getGeneratedKeys():" + autoIncKeyFromApi);} finally { if (rs != null) { try { rs.close(); } catch (SQLException ex) { // ignore } } if (stmt != null) { try { stmt.close(); } catch (SQLException ex) { // ignore } }}也有使用select LAST_INSERT_ID() 注意:并发可能会出现问题。示例:
Statement stmt = null;ResultSet rs = null;try { // // Create a Statement instance that we can use for // ‘normal‘ result sets. stmt = conn.createStatement(); // // Issue the DDL queries for the table for this example // stmt.executeUpdate("drop TABLE IF EXISTS autoIncTutorial"); stmt.executeUpdate( "CREATE TABLE autoIncTutorial (" + "priKey INT NOT NULL AUTO_INCREMENT, " + "dataField VARCHAR(64), PRIMARY KEY (priKey))"); // // insert one row that will generate an AUTO INCREMENT // key in the ‘priKey‘ field // stmt.executeUpdate( "insert INTO autoIncTutorial (dataField) " + "values (‘Can I Get the Auto Increment Field?‘)"); // // Use the MySQL LAST_INSERT_ID() // function to do the same thing as getGeneratedKeys() // int autoIncKeyFromFunc = -1; rs = stmt.executeQuery("select LAST_INSERT_ID()"); if (rs.next()) { autoIncKeyFromFunc = rs.getInt(1); } else { // throw an exception from here } System.out.println("Key returned from " + "‘select LAST_INSERT_ID()‘: " + autoIncKeyFromFunc);} finally { if (rs != null) { try { rs.close(); } catch (SQLException ex) { // ignore } } if (stmt != null) { try { stmt.close(); } catch (SQLException ex) { // ignore } }}mybatis封装后的配置如下:
调用
postDao.add(post);和以前一样结果后返回1,使用post.getId()可以获取到自增的id。参考文献:
【1】http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-last-insert-id.html
【2】http://stackoverflow.com/questions/12241260/get-auto-genearated-key-for-the-inserted-record-in-mybatis
一.单表查询1.查询所有数据(select * FROM tb_name;)select 后面跟上要查询的字段,* 号代表所有的字段,一般来说,查询所有字段是最耗时长的,所以今后查询数据尽可能按需索取...
2024.11.26查各表数据量 -- 查各表数据量SELECTtable_schema AS ‘数据库名‘,table_name AS ‘表名‘,TABLE_COMMENT AS ‘表注释‘,CREATE_TIME A...
2024.12.02SQLServer是一个可扩展的、高性能的、为分布式客户机/服务器计算所设计的数据库管理系统,实现了与WindowsNT的有机结合,提供了基于事务的企业级信息管理系统方案。SQL的优点有:1、高性能设...
2024.12.02对于表中已经有数据了,但是因为一些意外原因,导致表中的自增长ID不连续、或即将要超出ID长度了,在不影响表中数据的情况下,可以对表中的自增长主键ID进行重置。对如下表中的自增长ID进行重置:表中ID出...
2024.12.03简介在处理数据的时候,经常会在MySQL里面格式化时间进行统计。DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据。用法select DATE_FORMAT(date,format) ...
2024.12.02