public static void main(String[] args) {
// TODO Auto-generated method stub
String driver = "oracle.jdbc.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.0.120:1521:orcl";// 127.0.0.1是本机地址,XE是精简版Oracle的默认数据库名
String user = "spjx";// 用户名,系统默认的账户名
String password = "123456";// 你安装时选设置的密码
Connection con = null;// 创建一个数据库连接
PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
ResultSet selectsql = null;// 创建一个结果集对象
ResultSet analyzesql= null;
try
{
Class.forName(driver);// 加载Oracle驱动程序
System.out.println("开始尝试连接数据库!");
con =DriverManager.getConnection(url, user, password);// 获取连接
System.out.println("连接成功!");
String sql = "select table_name from user_tables ";//查询所有表
System.out.println(sql);
pre =con.prepareStatement(sql);
selectsql=pre.executeQuery();
while(selectsql.next()) {
String table_name=selectsql.getString("TABLE_NAME");
String sql1="analyze table "+table_name+" compute statistics";//拼装分析表语句
pre =con.prepareStatement(sql1);
analyzesql=pre.executeQuery();
System.out.println(table_name);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
// 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
// 注意关闭的顺序,最后使用的最先关闭
if (analyzesql != null)
analyzesql.close();
if (selectsql != null)
selectsql.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("数据库连接已关闭!");
}
catch (Exception e)
{
System.out.println("数据库关闭异常!");
e.printStackTrace();
}
}
}
利用Java自动分Oracle表(用于处理导出时少表的情况)
最新推荐文章于 2021-04-16 02:54:25 发布