博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JDBC basic steps

Posted on 2006-07-12 20:08  daniel-shen  阅读(154)  评论(0编辑  收藏  举报
import java.net.*;
import java.sql.*;
import java.io.*;
import java.util.*;
class MakeDB{
 public static void main(String args[]){
  try
  {
   Class.forName("com.mysql.jdbc.Driver");
   String url="jdbc:mysql://192.168.4.90:3306/i_collabo";
   String user="root";
   String password="admin";
   Connection con=DriverManager.getConnection(url,user,password);
   Statement stmt = con.createStatement();
   String fileName="";
   if(args.length>0)
    fileName=args[0];
   else
   {
    System.out.println("enter filename:");
    fileName = new DataInputStream(System.in).readLine();
   }
   int i=0;
   while(i<fileName.length()&&
     Character.isLowerCase(fileName.charAt(i)) ||
     Character.isUpperCase(fileName.charAt(i)))
     i++;
   String tableName = fileName.substring(0,i);
   DataInputStream in = new DataInputStream(new FileInputStream(fileName));
   String[] columnNames = readLine(in);
   String[] columnTypes = readLine(in);
   createTable(stmt,tableName,columnNames,columnTypes);
   boolean done = false;
   while(!done)
   {
    String[] values=readLine(in);
    if(values.length==0) done = true;
    else insertInto(stmt,tableName,columnTypes,values); 
   }
   showTable(stmt,tableName,columnNames.length);
   stmt.close();
   con.close();
  }catch(Exception e){
   
  }
 }
 private static String[] readLine(DataInput in) throws IOException{
  String line=in.readLine();
  Vector result = new Vector();
  if(line !=null)
  {
   StringTokenizer t = new StringTokenizer(line,"|");
   while (t.hasMoreTokens())
    result.addElement(t.nextToken().trim());
  }
  String[] retval=new String[result.size()];
  result.copyInto(retval);
  return retval;
 }
 private static void createTable(Statement stmt,String tableName,
   String  columnNames,String[] columnTypes) throws SQLException{
  String command="CREATE TABLE "+tableName+" (\n";
  String primary="";
  for(int i=0;i<columnNames.length;i++){
   if(i>0) command+=",\n";
   String columnName=columnNames[i];
   if(columnName.charAt(0)=='*')
   {
    if(primary.length()>0) primary+=", ";
    columnName=columnName.substring(1,columnName.length());
    primary+=columnName;
   }
   command+=columnName+" "+columnTypes[i];
  }
  if(primary.length()>0) command+="\nPRIMARY KEY("+primary+")";
  command+=")\n";
  stmt.executeUpdate(command);
 }
 private static void insertInto(Statement stmt,String tableName,
   String[] columnTypes,String[] values) throws SQLException{
  String command = "INSERT INTO "+tableName+" VALUES(";
  for (int i = 0;i<columnTypes.length;i++){
   if(i>0)command+=", ";
   String columnType = columnTypes[i].toUpperCase();
   String value="";
   if(i<values.length)value=values[i];
   if(columnType.startsWith("CHAR")||columnType.startsWith("VARCHAR")){
    int from=0;
    int to=0;
    command +="'";
    while((to=value.indexOf('\'',from))>=0)
    {
     command+=value.substring(from,to)+"''";
     from=to+1;
    }
    command+=value.substring(from)+"'";
   }
   else command+=value;
  }
  command+=")";
  stmt.executeUpdate(command);
 }
 private static void showTable(Statement stmt,String tableName,
   int numCols)throws SQLException
   {
    String query="SELECT * FROM "+tableName;
    ResultSet rs=stmt.executeQuery(query);
    while(rs.next())
    {
     for(int i=1;i<=numCols;i++)
     {
      if(i>1)System.out.print("|");
      System.out.print(rs.getString(i));
     }
     System.out.println("");
    }
    rs.close();
   }
}