Java Simple link Oracle database registration and login function of the implementation code _java

Source: Internet
Author: User

Copy Code code as follows:

Basic information for user users and 3 columns in the UserInfo table
Package landing judgment;

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 blocks of code for registration and landing
Package landing judgment;

Import Java.util.Scanner;

public class in {

public static User GetUser () {
User U = new user ();
Scanner sc = new Scanner (system.in);
System.out.println ("Please enter login name");
U.setloginname (Sc.nextline ());
System.out.println ("Please enter the password");
U.SETLOGINPSW (Sc.nextline ());
return u;
}
public static User RegisterUser () {
User U = new user ();
Scanner sc = new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("Please register user name");
U.setname (Sc.nextline ());
SYSTEM.OUT.PRINTLN ("Please register login name");
U.setloginname (Sc.nextline ());
SYSTEM.OUT.PRINTLN ("Please register password");
U.SETLOGINPSW (Sc.nextline ());
return u;
}
}

Check when landing, to determine whether the login name and password is correct, correct return username
Package landing 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: 1521: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.executequery ();
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 ("Landing failure");
}else{
System.out.println ("Welcome" +u.getname () + "landing");
}

}
}

Registration interface, to determine whether the login has a duplicate, there is a duplication of registration failure. There's a little doubt that has been marked out with comments
Package landing 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;

Determine if 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 ());
Determine if the login name already exists
s = conn.createstatement ();
rs = S.executequery (SQL_CK);
while (Rs.next ()) {//Why rs.getstring (2) prompts an invalid index?
if (rs.getstring ("LOGINNAME"). Equals (U.getloginname ())) {
SYSTEM.OUT.PRINTLN ("Login name already exists, registration failed");
Break
}else{
Pst.executeupdate ();
SYSTEM.OUT.PRINTLN ("registered success");
Break
}
}
catch (SQLException e) {
E.printstacktrace ();
}finally{
try {
Rs.close ();
Pst.close ();
S.close ();
Conn.close ();
catch (SQLException e) {
E.printstacktrace ();
}
}


}
}
Main interface//
The structure of the table in the database so
Name (user name, for display),
LoginName (login name, ID entered at login)
LOGINPSW (password entered at login)
Package landing judgment;

Import Java.util.Scanner;

public class Test {

public static void Main (string[] args) {

int chos = 0;
Scanner sc = new Scanner (system.in);
SYSTEM.OUT.PRINTLN ("\t\t login please press the 1\t\t register please press 2");
Chos = Sc.nextint ();
Switch (chos) {
Case 1:check.checkuser (In.getuser ());
Case 2:register.registuser (In.registeruser ());
Default:System.out.println ("Please enter the right choice");
}
}

}

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.