使用statement 接口对象,进行以下操作:
- 显示数据库版本号
- 显示所有库
- 显示所有库中的table表
显示数据库版本号:
public class StatementDemo {
@Test
void showall(){
try{
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select version()");
rs.next();
System.out.println(rs.getString(1));
}catch (Exception e){
e.printStackTrace();
}
}
}
执行结果:
显示mysql版本号,所有库和table表名:
@Test
void showall(){
try{
Statement st = conn.createStatement();
Statement st2 = conn.createStatement();
ResultSet rs = st.executeQuery("select version()");
rs.next();
System.out.println(rs.getString(1));
rs = st.executeQuery("show databases");
while (rs.next()){
String dn = rs.getString(1);
System.out.println("数据库:"+dn);
ResultSet rs2 = st2.executeQuery("show tables from " +dn);
while (rs2.next()){
System.out.println("table:" +rs2.getString(1));
}
System.out.println("--------------------------------------------\n");
rs2.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
执行结果: