MySQL last add +java connection database

Source: Internet
Author: User

Truncate Table Name

Deleting all the data in the table directly, unlike Delete, this method cannot use the where selection and can only be deleted.

TRUNCATE table users;

Java Connection database:

Package Com.edu.gkh;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.Statement;
Import Java.util.Scanner;

public class Homework {
Static Statement Sc=null;
Static Scanner sca=new Scanner (system.in);
static String username;
public static void Main (string[] args) throws Exception {
Getstatement ();
String name2= "";
String pwd2= "";
for (;;) {
System.out.println ("Please enter a registered account");
String Name=sca.next ();
System.out.println ("Please enter password");
String Pwd=sca.next ();
System.out.println ("Please enter your password again");
String Pwd1=sca.next ();
if (Pwd1.equals (pwd)) {
Name2=name;
Pwd2=pwd;
SYSTEM.OUT.PRINTLN ("registered success");
Break
}
else{
System.out.println ("Two times password inconsistent, please re-enter; re-register yes/no");
Char Regist=sca.next (). charAt (0);
if (regist== ' Y ') {
SYSTEM.OUT.PRINTLN ("Please re-enter");
}
else if (regist== ' n ') {
SYSTEM.OUT.PRINTLN ("You have withdrawn from registration");
}
else{
SYSTEM.OUT.PRINTLN ("Your input is illegal");
}
}
}

System.out.println ("Welcome to J18 Bank");
SYSTEM.OUT.PRINTLN ("Please login:");
System.out.println ("User name:");
Username=sca.next ();
System.out.println ("Password:");
String Password=sca.next ();
int Num=queryaccount (Username,password);
if (num==1) {
System.out.println ("Landing success");
for (;;) {
SYSTEM.OUT.PRINTLN ("Please select a transaction type:");
System.out.println ("1, save 2, take the money 3, check the balance");
int Cz=sca.nextint ();
if (cz==1) {
Cun ();
}else if (cz==2) {
Qu ();
}else if (cz==3) {
Query ();
}else{
SYSTEM.OUT.PRINTLN ("Thank you for using!" ");
Break
}
}
}else{
SYSTEM.OUT.PRINTLN ("Login Failed");
}
}

public static void Cun () throws exception{
System.out.println ("Please enter your deposit amount:");
Double money=sca.nextdouble ();
String sql= "Update account set money=money+" +money;
Boolean a=sc.execute (SQL);
if (!a) {
System.out.println ("Deposit success! ");
}
}

public static void Qu () throws exception{
System.out.println ("Please enter your withdrawal amount:");
Double money=sca.nextdouble ();
String sql= "Update account set money=money-" +money;
SYSTEM.OUT.PRINTLN (SQL);
Boolean a=sc.execute (SQL);
if (!a) {
SYSTEM.OUT.PRINTLN ("Withdrawals are successful! ");
}
}

public static int Queryaccount (String username,string password) throws exception{
String sql= "SELECT * from account where aname= '" +username+ "' and apwd= '" +password+ "'";
ResultSet rs=sc.executequery (SQL);
int num=0;
while (Rs.next ()) {
num++;
}
return num;
}

public static void query () throws exception{
String sql= "Select money from account where Aname= '" +username+ "";
ResultSet rs=sc.executequery (SQL);
Double money=0;
while (Rs.next ()) {
Money=rs.getdouble (1);
}
System.out.println ("Your account Balance:" +money);
}


public static void Getstatement () throws exception{
Class.forName ("Com.mysql.jdbc.Driver");
String url= "JDBC:MYSQL://LOCALHOST:3306/ATM";
String username= "root";
String password= "root";
Connection c=drivermanager.getconnection (Url,username,password);
Sc=c.createstatement ();
}
}

MySQL last add +java connection database

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.