日期:2014-05-16  浏览次数:20988 次

请问这段代码哪里错了 为什么我找不出来

package jdbc;
import java.io.*;
import java.sql.*;
public class ExecuteSQL {
public static void main(String[] args) {
Connection conn = null;
try{
String driver = null,url=null,user="",password="";
for(int n=0;n<args.length;n++){
if(args[n].equals("-d")) driver=args[++n];
else if (args[n].equals("-u")) user=args[++n];
else if (args[n].equals("-p")) password=args[++n];
else if (url== null) url=args[n];
else throw new IllegalArgumentException("Unknow arugument.");
}
if (url== null) throw new IllegalArgumentException("No database specifid");
if (driver!=null) Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
Statement s = conn.createStatement();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.print("sql> ");
System.out.flush();
String sql = in.readLine();
if ((sql==null)||sql.equals("quit")) break;
if (sql.length() == 0) continue;
try{
boolean status = s.execute(sql);
do{
if (status){
ResultSet rs = s.getResultSet();
printResultsTable(rs,System.out);
}
else{
int numUpdates = s.getUpdateCount();
System.out.println("ok. "+ numUpdates + " rows affected.");
}
status = s.getMoreResults();
} while(status || s.getUpdateCount() != -1);
}
catch (SQLException e){
System.err.println("SQLException: "+e.getMessage()+":"+e.getSQLState());
}
finally{
SQLWarning w;
for(w=conn.getWarnings();w!=null;w=w.getNextWarning())
System.err.println("WARNING: "+w.getMessage()+":"+w.getSQLState());
}
}
}
catch (SQLException e){
System.err.println(e);
if (e instanceof SQLException)
System.err.println("SQL State: "+((SQLException)e).getSQLState());
System.err.println("Usage: java ExecuteSQL [-d <driver>] "+"[-u <user>] [-p <password>] <database URL>");
}
finally{
try{ conn.close();} catch (Exception e) {}
}
}
static void printResultsTable(ResultSet rs,OutputStream output)throws SQLException
{
PrintWriter out = new PrintWriter(output);
ResultSetMetaData metadata = rs.getMetaData();
int numcols = metadata.getColumnCount();
String[] labels = new String[numcols];
int[] colwidths = new int[numcols];
int[] colpos = new int[numcols];
int linewidth;
linewidth = 1;
for(int i=0;i<numcols;i++){
colpos[i]=linewidth;
labels[i]=metadata.getColumnLabel(i+1);
int size = metadata.getColumnDisplaySize(i+1);
if (size==-1)size=30;
if (size>500)size=30;
int labelsize = labels[i].length();
if (labelsize>size)size=labelsize;
colwidths[i]=size+1;
linewidth += colwidths[i]+2;
}
StringBuffer divider = new StringBuffer(linewidth);
StringBuffer blankline = new StringBuffer(linewidth);
for(int i=0;i<linewidth;i++){
divider.insert(i,'-');
blankline.insert(i," ");
}
for(int i=0;i<numcols;i++) divider.setCharAt(colpos[i]-1,'+');
divider.setCharAt(linewidth-1,'+');
out.println(divider);
StringBuffer line = new StringBuffer(blankline.toString());
line.setCharAt(0,'|');
for(int i=0;i<numcols;i++){
int pos = colpos[i]+1+(colwidths[i]-labels[i].length())/2;
overwrite(line,pos,labels[i]);
o