Every time I was asked during an interview, the JDBC Data Link process was stuck. This time, I am not afraid of it... First, rough Try { Class. forname ("com. MySQL. JDBC. Driver "); } Catch (classnotfoundexception e ){} // Define the three database application objects to be used Connection con = NULL; // connection object Statement SQL = NULL; // statement object (SQL statement) Resultset rs = NULL; // result set object // Connect the data source Try { Con = drivermanager. getconnection ("JDBC: mysql: // localhost/scutcs", "", ""); // URL user name and password for database connection SQL = con. createstatement (); String to = "select * From user1 where username = '" + username + "'"; Rs= SQL .exe cutequery (to); // The result set is generated and stored in RS according to the defined statement. If (Rs. Next () // determines whether the result set is null. If not, a record exists. { Out. Print ("<SCRIPT> alert ('user name" + XM + "already exists. Please select another one! '); History. Back (); </SCRIPT> "); // return to the registration page if any } Else {Add a record to the database if it does not exist} } Catch (sqlexception E) {Out. Print (E ); }
Second, as a public class, it is more complete to define the part as a single function for convenient calls. Import java. SQL .*;Public class dB { Public static connection getconn (){ Connection conn = NULL; Try { Class. forname ("com. MySQL. JDBC. Driver "); Conn = drivermanager. getconnection ("JDBC: mysql: // localhost/shopping? User = root & Password = 123456 "); } Catch (classnotfoundexception e ){ E. printstacktrace (); } Catch (sqlexception e ){ E. printstacktrace (); } Return conn; } Public static preparedstatement prepare (connection Conn, string SQL ){ Preparedstatement pstmt = NULL; Try { If (Conn! = NULL ){ Pstmt = conn. preparestatement (SQL ); } } Catch (sqlexception e ){ E. printstacktrace (); } Return pstmt; } Public static preparedstatement prepare (connection Conn, string SQL, int autogenereatedkeys ){ Preparedstatement pstmt = NULL; Try { If (Conn! = NULL ){ Pstmt = conn. preparestatement (SQL, autogenereatedkeys ); } } Catch (sqlexception e ){ E. printstacktrace (); } Return pstmt; } Public static statement getstatement (connection conn ){ Statement stmt = NULL; Try { If (Conn! = NULL ){ Stmt = conn. createstatement (); } } Catch (sqlexception e ){ E. printstacktrace (); } Return stmt; } /* Public static resultset getresultset (connection Conn, string SQL ){ Statement stmt = getstatement (conn ); Resultset rs = getresultset (stmt, SQL ); Close (stmt ); Return Rs; } */ Public static resultset getresultset (statement stmt, string SQL ){ Resultset rs = NULL; Try { If (stmt! = NULL ){ Rs = stmt.exe cutequery (SQL ); } } Catch (sqlexception e ){ E. printstacktrace (); } Return Rs; } Public static void executeupdate (statement stmt, string SQL ){ Try { If (stmt! = NULL ){ Stmt.exe cuteupdate (SQL ); } } Catch (sqlexception e ){ E. printstacktrace (); } } Public static void close (connection conn ){ Try { If (Conn! = NULL ){ Conn. Close (); Conn = NULL; } } Catch (sqlexception e ){ E. printstacktrace (); } } Public static void close (statement stmt ){ Try { If (stmt! = NULL ){ Stmt. Close (); Stmt = NULL; } } Catch (sqlexception e ){ E. printstacktrace (); } } |