📄 createdb.java
字号:
/**
* CreateDB.java
*
*/
import java.net.*;
import java.sql.*;
import java.io.*;
import java.util.*;
class CreateDB
{ public static void main (String args[])
{ try
{ Connection con = getConnection();
Statement stmt = con.createStatement();
String tableName = "";
if (args.length > 0)
tableName = args[0];
else
{ System.out.println("Usage: CreateDB TableName");
System.exit(0);
}
BufferedReader in = new BufferedReader(new
FileReader(tableName + ".dat"));
createTable(tableName, in, stmt);
showTable(tableName, stmt);
in.close();
stmt.close();
con.close();
}
catch (SQLException ex)
{ System.out.println ("SQLException:");
while (ex != null)
{ System.out.println ("SQLState: "
+ ex.getSQLState());
System.out.println ("Message: "
+ ex.getMessage());
System.out.println ("Vendor: "
+ ex.getErrorCode());
ex = ex.getNextException();
System.out.println ("");
}
}
catch (IOException ex)
{ System.out.println("Exception: " + ex);
ex.printStackTrace ();
}
}
public static Connection getConnection()
throws SQLException, IOException
{ Properties props = new Properties();
String fileName = "CreateDB.properties";
FileInputStream in = new FileInputStream(fileName);
props.load(in);
String drivers = props.getProperty("jdbc.drivers");
if (drivers != null)
System.setProperty("jdbc.drivers", drivers);
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
return
DriverManager.getConnection(url, username, password);
}
public static void createTable(String tableName,
BufferedReader in, Statement stmt)
throws SQLException, IOException
{ String line = in.readLine();
String command = "CREATE TABLE " + tableName
+ "(" + line + ")";
stmt.executeUpdate(command);
while ((line = in.readLine()) != null)
{ command = "INSERT INTO " + tableName
+ " VALUES (" + line + ")";
stmt.executeUpdate(command);
}
}
public static void showTable(String tableName,
Statement stmt) throws SQLException
{ String query = "SELECT * FROM " + tableName;
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs.next())
{ for (int i = 1; i <= columnCount; i++)
{ if (i > 1) System.out.print(", ");
System.out.print(rs.getString(i));
}
System.out.println();
}
rs.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -