java语言如何动态循环创建mysql数据库基本表,可以实现吗?

java语言如何动态循环创建mysql数据库基本表,可以实现吗?,第1张

下面是一个简单的连接MySQL数据库,并 *** 作数据库表的例子:

import java.sql.*

public class TestMysql {

public static void main(String[] args) {

Connection conn = null

Statement stmt = null

ResultSet rs = null

try {

//加载驱动

Class.forName("com.mysql.jdbc.Driver")

//创建连接

conn = DriverManager

.getConnection("jdbc:mysql://localhost/bbs?user=用户名&password=密码")

stmt = conn.createStatement()

rs = stmt.executeQuery("select * from user")

while (rs.next()) {

String user = rs.getString(1)

String password = rs.getString(2)

System.out.println("用户名:" + user + "," +" 密码:" + password )

}

} catch (ClassNotFoundException ex) {

ex.printStackTrace()

} catch (SQLException ex) {

ex.printStackTrace()

} finally {

try {

if (rs != null) {

rs.close()

rs = null

}

if (stmt != null) {

stmt.close()

stmt = null

}

if (conn != null) {

conn.close()

conn = null

}

} catch (SQLException e) {

e.printStackTrace()

}

}

}

}

当然前提是要把MySQL的连接驱动JAR文件添加到工程里

用个简单的循环咯:

用php编程给你举例吧:

for($i=1$i<=100$i ){

$sql="CREATE TABLE a".$i." .... "

mysql_query($sql)

}

就这样拼接sql语句。加油,祝你成功

怎么循环创建mysql表分区和清空表分区

如果是mysql5.5还是可以做到的,5.1不行

CREATE TABLE part_date

( c1 bigint(20) unsigned NOT NULL AUTO_INCREMENT,

c2 varchar(40) not null default '',

c3 datetime not NULL,

PRIMARY KEY (c1,c3),

KEY partidx(c3)) ENGINE=InnoDB DEFAULT CHARSET=utf8

partition by range COLUMNS(c3)

(

PARTITION p201012 VALUES LESS THAN ('2011-01-01 06:00:00'),

PARTITION p201101 VALUES LESS THAN ('2011-01-01 12:00:00'),

PARTITION p201102 VALUES LESS THAN ('2011-01-01 18:00:00'),

PARTITION p201103 VALUES LESS THAN ('2011-01-01 23:59:59'),

PARTITION p201912 VALUES LESS THAN MAXVALUE )

然后用函数录入数据

DELIMITER $$

DROP PROCEDURE IF EXISTS `load_data` $$

CREATE DEFINER=`root`@`%` PROCEDURE `load_data`()

BEGIN

declare v int default 0

while v <10000

do

insert into part_date(c2,c3)

values (uuid(),'2011-01-01 01:00:00')

insert into part_date(c2,c3)

values (uuid(),'2011-01-01 03:00:00')

insert into part_date(c2,c3)

values (uuid(),'2011-01-01 05:01:00')

insert into part_date(c2,c3)

values (uuid(),'2011-01-01 07:01:00')

insert into part_date(c2,c3)

values (uuid(),'2011-01-01 15:01:00')

insert into part_date(c2,c3)

values (uuid(),'2011-01-01 16:01:00')

insert into part_date(c2,c3)

values (uuid(),'2011-01-01 17:01:00')

insert into part_date(c2,c3)

values (uuid(),'2011-01-01 18:01:00')

insert into part_date(c2,c3)

values (uuid(),'2011-01-01 19:01:00')

set v = v + 1

end while

END $$

DELIMITER

分区

explain partitions select count(*) from part_date where c3 >date '2011-01-01 06:02:00' and c3 <date '2011-01-01 08:02:00'

看一下只走了p201101分区


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/6145863.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-16
下一篇 2023-03-16

发表评论

登录后才能评论

评论列表(0条)

保存