JAVA is a simple link to the Oracle database for registration and login.

Source: Internet
Author: User

// The basic information of the User, which is also the package logon judgment of the three columns in the USERINFO table; public class User {private String name; private String loginname; private String loginpsw; public String getName () {return name;} public void setName (String name) {this. name = name;} public String getLoginname () {return loginname;} public void setLoginname (String loginname) {this. loginname = loginname;} public String getLoginpsw () {return loginpsw;} public void setLoginpsw (String loginpsw) {this. loginpsw = loginpsw ;}}
// Small code block for registration and login package login judgment; import java. util. extends; public class In {public static User getUser () {User u = new User (); Using SC = new using (System. in); System. out. println ("Enter the login name"); u. setLoginname (SC. nextLine (); System. out. println ("Enter Password"); u. setLoginpsw (SC. nextLine (); return u;} public static User registerUser () {User u = new User (); Limit SC = new limit (System. in); System. out. println ("Please register username"); u. setName (SC. nextLine (); System. out. println ("Please register the login name"); u. setLoginname (SC. nextLine (); System. out. println ("Please register password"); u. setLoginpsw (SC. nextLine (); return u ;}}

// Check during login to check whether the login name and password are correct. If the login is correct, return the user name package logon judgment; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; public class Check {public static void checkUser (User u) {try {Class. forName ("oracle. jdbc. driver. oracleDriver ");} catch (ClassNotFoundException e) {e. printStackTrace ();} String url = "jdbc: oracle: thin: @ localhost: 1 521: orcl "; String user =" scott "; String password =" tiger "; Connection conn = null; ResultSet rs = null; PreparedStatement pst = null; string SQL = "Select name from USERINFO where loginname =? And loginpsw =? "; Try {conn = DriverManager. getConnection (url, user, password); pst = conn. prepareStatement (SQL); pst. setString (1, u. getLoginname (); pst. setString (2, u. getLoginpsw (); rs = pst.exe cuteQuery (); while (rs. next () {u. setName (rs. getString (1) ;}} catch (SQLException e) {e. printStackTrace ();} finally {try {rs. close (); pst. close (); conn. close ();} catch (SQLException e) {e. printStackTrace () ;}} if (u. getName () = null | u. getName (). isEmpty () {System. out. println ("Logon Failed");} else {System. out. println ("welcome" + u. getName () + "login ");}}}

// On the registration page, check whether there are duplicate login names. If there are duplicate login names, the registration fails. I have a few questions. I have commented out the package for login and judgment; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; public class Register {public static void registUser (User u) {try {Class. forName ("oracle. jdbc. driver. oracleDriver ");} catch (ClassNotFoundException e) {e. printStackTrace ();} String url = "jdbc: oracle: thin: @ Localhost: 1521: orcl "; String user =" scott "; String password =" tiger "; Connection conn = null; string SQL = "Insert Into USERINFO values (?,?,?) "; PreparedStatement pst = null; // judge whether the login name already exists Statement s = null; ResultSet rs = null; String SQL _ck =" Select LOGINNAME FROM USERINFO "; try {conn = DriverManager. getConnection (url, user, password); pst = conn. prepareStatement (SQL); pst. setString (1, u. getName (); pst. setString (2, u. getLoginname (); pst. setString (3, u. getLoginpsw (); // determines whether the logon name already exists. s = conn. createStatement (); rs = s.exe cuteQuery (SQL _ck); while (rs. next () {// Why is rs used. if (rs. getString ("LOGINNAME "). equals (u. getLoginname () {System. out. println ("login name already exists, registration failed" cannot begin break?else=pst.exe cuteUpdate (); System. out. println ("registered"); break ;}} catch (SQLException e) {e. printStackTrace ();} finally {try {rs. close (); pst. close (); s. close (); conn. close ();} catch (SQLException e) {e. printStackTrace ();}}}}

// Main interface /// this is the structure of the table in the database // name (user name, used for display), // loginname (login name, that is, the ID entered during login) // loginpsw (password entered during login) package logon judgment; import java. util. optional; public class Test {public static void main (String [] args) {int chos = 0; Using SC = new using (System. in); System. out. println ("\ t login press 1 \ t registration press 2"); chos = SC. nextInt (); switch (chos) {case 1: Check. checkUser (In. getUser (); break; case 2: Register. registUser (In. registerUser (); break; default: System. out. println ("enter the correct choice"); break ;}}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.