📄 register.java
字号:
package cs636;
import java.sql.*;
public class Register {
public static void main(String args[]) {
try {
if (args.length != 3) {
System.out.println("usage:java <oracle|mysql> <user> <passwd>");
System.exit(1);
}
String dbSpec = args[0];
String user = args[1];
String password = args[2];
String driver = null;
String connStr = null;
System.out.println("user = " + user + ", password = " + password);
if (dbSpec.equalsIgnoreCase("oracle")) {
driver = "oracle.jdbc.OracleDriver";
connStr = "jdbc:oracle:thin:@dbs2.cs.umb.edu:1521:dbs2";
} else if (dbSpec.equalsIgnoreCase("mysql")) {
driver = "com.mysql.jdbc.Driver";
connStr = "jdbc:mysql://fantasyland.cs.umb.edu/" + user + "db";
}
try {
Class.forName(driver); // load driver: it runs a static
// initializer that loads other classes, etc.
}catch (Exception e) {
System.out.println(e);
}
System.out.println("using connection string: " + connStr);
System.out.print("Connecting to the database...");
System.out.flush();
// Connect to the database
Connection conn = DriverManager.getConnection(connStr, user, password);
System.out.println("connected.");
// new Connection, start a try with a finally to close connection
try {
// Create a statement
Statement stmt = conn.createStatement();
try {
ResultSet rset = stmt.executeQuery("select max(pk_id) from users");
int newID = (rset.next()) ? (rset.getInt(1) + 1) : 1;
stmt.execute("insert into users values (" + newID + ",'ATD','liangch'," +
"'Chong','liangch','liangch@cs.umb.edu','857-891-9577','857-891-9577'," +
"'857-891-9577',3,NULL,NULL,NULL,NULL,NULL,NULL)");
rset = stmt.executeQuery("select * from users");
while (rset.next())
System.out.println(rset.getString(1));
} catch (SQLException e) {
System.out.println("Problem with welcome table");
while (e != null) {
e.printStackTrace();
e = e.getNextException();
}
}
System.out.println("Your registeration is succeddfully.");
} finally {
// Close the connection, no matter what happens above
conn.close(); // this also closes the Statement and ResultSet,
// if any
}
} catch (SQLException e) {
while (e != null) {
e.printStackTrace();
e = e.getNextException();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -