CSDN博客

img realmud

Auto-db

发表于2004/6/28 10:09:00  553人阅读

package sql;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class CreateSchema {

 public static void main(String[] args) throws Exception {
  //  readSchema();
  createSchema();
 }

 static void createSchema() throws Exception {

  Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
  String url = "jdbc:oracle:thin:@localhost:1521:john";
  String user = "inqint";
  String password = "inqint";
  Connection conn = DriverManager.getConnection(url, user, password);
  Statement stmt = conn.createStatement();
  //   String sql = new String(readSchema().getBytes("gb2312"),"ISO8859_1");
  String sql = readContent();
  String temp[] = sql.split(";");
  for (int i = 0; i < temp.length; i++) {
   System.out.println(temp[i].trim());
   stmt.execute(temp[i].trim());
  }

 }

 static String readSchema() {
  StringBuffer content = new StringBuffer();
  try {
   File file =
    new File("src/com/augmentum/inquira/setting/createTable.sql");
   FileInputStream fin = new FileInputStream(file);
   FileChannel fc = fin.getChannel();
   ByteBuffer buffer =
    ByteBuffer.allocate(new Long(file.length()).intValue());
   fc.read(buffer);

   for (int i = 0; i < buffer.limit() - 1; i++) {
    content.append(buffer.getChar(i));
   }

   //   System.out.println(content);
  } catch (Exception e) {
   System.out.println("exception: " + e.getMessage());
  }
  return content.toString();
 }

 static String readContent() {
  String fc = null;
  try {
   File file =
    new File("src/com/augmentum/inquira/setting/createTable.sql");
   BufferedInputStream bis = null;
   int remaining = (int) file.length();
   int offset = 0;
   int status = 0;
   byte rc[] = new byte[remaining];

   bis = new BufferedInputStream(new FileInputStream(file));

   while (remaining > 0
    && (status = bis.read(rc, offset, remaining)) >= 0) {
    offset += status;
    remaining -= status;
   }
   fc = new String(rc);
  } catch (Exception e) {
   System.out.println("exception: " + e.getMessage());
  }
  return fc;
 }

}

0 0

相关博文

我的热门文章

img
取 消
img