MYSQL默认的就是InnoDB
创建表的时候显示指定
create table a(id int not null auto_increment primary key,name varchar(255)) ENGINE=InnoDB;
或
create table b(id int not null auto_increment primary key,name varchar(255)) ENGINE=MyISAM;
SHOW TABLE STATUS;命令可以看到表的类型
使用ALTER命令可以对单个表的类型进行修改
ALTER TABLE talbe_name ENGINE=InnoDB/MyISAM;
可以看到MySQL当前默认的新生成表的类型。
SHOW GLOBAL VARIABLES LIKE '%engine%';
JDBC有这个API,Statement.getGeneratedKeys();
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection ("jdbc:mysql://localhost:3306/testid","root","root");
// 创建带问号的参数化语句
String template = "insert into t_test(name) values(?) ";
PreparedStatement statement = connection.prepareStatement (template);
statement.setString(1, "langhua1");
statement.execute();
ResultSet rs = statement.getGeneratedKeys();
while(rs.next()){
//获得主键
System.out.println(rs.getInt(1));
}
statement.close();
rs.close();
connection.close();
connection.setAutoCommit(false);
Statement stat = connection.getStatement();
stat.executeUpdate("insert into test (name,password) values('123','321')");
ResultSet rs = stat.executeQuery("select id from test where name='123'");
rs.next();
System.out.println(rs.getInt('id'));
connection.commit();