Skip to content

Instantly share code, notes, and snippets.

@sourabhxyz
Created March 19, 2019 11:01
Show Gist options
  • Save sourabhxyz/7f3e7e710b28a48d416aef42d8076e41 to your computer and use it in GitHub Desktop.
Save sourabhxyz/7f3e7e710b28a48d416aef42d8076e41 to your computer and use it in GitHub Desktop.
package simpledb.server;
import simpledb.remote.*;
import java.rmi.registry.*;
import java.sql.*;
public class Startup {
public static void main(String args[]) throws Exception {
// configure and initialize the database
if (args.length < 3) {
System.out.print("Error: Enter Username and Password\n");
return;
}
SimpleDB.init(args[0]);
// create a registry specific for the server on the default port
Registry reg = LocateRegistry.createRegistry(1099);
// and post the server entry in it
RemoteDriver dd = new RemoteDriverImpl();
reg.rebind("simpledb", dd);
System.out.println("database server ready");
Connection conn = null;
Driver d = new SimpleDriver();
conn = d.connect("jdbc:simpledb://localhost", null);
Statement stmt = conn.createStatement();
String s = "create table Users(ID varchar(5), Pass varchar(20))";
stmt.executeUpdate(s);
System.out.println("Table Users created.");
s = "insert into Users(ID, Pass) values ";
String[] uservals = {"('root', 'root')", "('sourabh', 'hi')"};
for (int i = 0; i < uservals.length; i++) {
stmt.executeUpdate(s + uservals[i]);
}
s = "select ID, Pass from Users where ID = '" + args[1] + "' and Pass = '" + args[2] + "'";
ResultSet rs = stmt.executeQuery(s);
int cnt = 0;
while (rs.next()) {
cnt++;
}
rs.close();
if (cnt > 0) {
System.out.println("Success");
} else {
System.out.println("Failure");
System.exit(0);
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment