register.java

来自「hibernate for ORM, suit for hsql/mysql/o」· Java 代码 · 共 77 行

JAVA
77
字号
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 + =
减小字号Ctrl + -
显示快捷键?