There are two ways to connect to a database, direct connect and bridge, here the author only talk about the universal direct connection, bridge even readers want to know please Baidu
First of all to have the driver software, the author of the tool is very old
Create a folder lib in your Java project, copy the jar file into it, right click on the jar file →build path→configure Build path→
Add a jar file found in the Lib directory OK, the bottle will appear, so the drive is ready, and the connection starts below
Please note that all the packages are under java.sql, root is your MySQL account, 610321520 is your MySQL password, 3306 is the port number of MySQL, localhost is a palindrome address can also be written as 127.0.0.1
Load Driver
Class.forName ("Com.mysql.jdbc.Driver");
Registration driver
Connection coo=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/school", "root", "610321520");
Generating statement processing objects
Statement sta=coo.createstatement (); an important part of connecting a database
Here's what I did. A simple ATM connection database
First create a library in MySQL
CREATE DATABASE Bank
Use Bank
CREATE TABLE Users (
u_id INT,
U_name VARCHAR (50),
U_password INT,
U_money DOUBLE)
INSERT into Users VALUES (1, ' Xiao Wang ', 111,5000)
INSERT into Users VALUES (2, ' Floret ', 222,5000)
INSERT into Users VALUES (3, ' xiaoming ', 333,5000)
Here is the Java code
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import Java.util.Scanner;
public class ATM {
public static void Main (string[] args) {
Connection con = null;
Statement sta = null;
ResultSet rs = null;
int YH;
int mm;
try {
Class.forName ("Com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
String url = "Jdbc:mysql://localhost:3306/bank";
try {
System.out.println ("Normal operation of the system");
con = drivermanager.getconnection (URL, "root", "610321520");
// System.out.println (con);
STA = Con.createstatement ();
System.out.println ("Welcome to Bank of China");
int count = 0;
while (true) {
System.out.println ("Please enter your account");
Scanner in = new Scanner (system.in);
YH = In.nextint ();
System.out.println ("Please enter password");
Scanner in1 = new Scanner (system.in);
mm = In.nextint ();
String sql = "SELECT * from users where u_id = '" +yh+ "' and U_password = '" +mm+ "'";
rs = sta.executequery (SQL);
if (Rs.next ()) {
Break
}else {
Count + +;
if (count = = 3) {
SYSTEM.OUT.PRINTLN ("You have entered more than the number of security times, please bring valid documents to the counter to collect the card");
System.exit (0);
}else {
System.out.println ("Username or password is incorrect!");
}
}
}
while (true) {
String s = "Select U_name from users where u_id = '" +yh+ "";
rs = Sta.executequery (s);
String name = NULL;
if (Rs.next ()) {
Name = Rs.getstring ("U_name");
System.out.println (name+ "Mr. Hello! Please select the action button: 1. Deposit 2. Withdrawals 3. Check the Balance 4. Change the password 5. Transfer 6. Return card ");
}
Scanner in = new Scanner (system.in);
int flag = In.nextint ();
if (flag = = 1) {
System.out.println ("Please enter your deposit amount");
Scanner in1 = new Scanner (system.in);
int C_money = In1.nextint ();
String sql = "Update users set U_money=u_money + '" +c_money+ "' Where u_id= '" +yh+ "'";
Sta.executeupdate (SQL);
System.out.println ("deposit success");
}else if (flag = = 2) {
System.out.println ("Please enter your withdrawal amount");
Scanner in1 = new Scanner (system.in);
int Q_money =in1.nextint ();
String SQL1 = "Select U_money from Users where u_id= '" +yh+ "";
rs = Sta.executequery (SQL1);
int money = 0;
while (Rs.next ()) {
Money = Rs.getint (1);
}
if (Money >= Q_money) {
String sql = "Update users set U_money=u_money-'" +q_money+ "' Where u_id= '" +yh+ "'";
Sta.executeupdate (SQL);
System.out.println ("successful withdrawal");
}else {
System.out.println ("Your balance is insufficient, please check the balance");
}
}else if (flag = = 3) {
String SQL1 = "Select U_money from Users where u_id= '" +yh+ "";
rs = Sta.executequery (SQL1);
int money = 0;
while (Rs.next ()) {
Money = Rs.getint (1);
}
System.out.println ("Your balance is:" + Money + "Yuan");
}else if (flag = = 4) {
System.out.println ("Please enter the old password");
Scanner in1 = new Scanner (system.in);
int jmm = In1.nextint ();
if (jmm = = mm) {
System.out.println ("Please enter a new password");
Scanner in2 = new Scanner (system.in);
int XMM = In1.nextint ();
String sql = "Update users set u_password= '" +xmm+ "' Where u_id= '" +yh+ "'";
Sta.executeupdate (SQL);
SYSTEM.OUT.PRINTLN ("Change password success");
}else {
SYSTEM.OUT.PRINTLN ("You entered the wrong password, please re-enter");
}
}else if (flag = = 5) {
System.out.println ("Please enter the user you want to transfer");
Scanner in1 = new Scanner (system.in);
String zh1 = In1.next ();
SYSTEM.OUT.PRINTLN ("Please re-enter the user you want to transfer");
Scanner in2 = new Scanner (system.in);
String zh2 = In1.next ();
if (Zh1.equals (ZH2)) {
String sql = "Select U_name from Users where u_id= '" +yh+ "";
rs = sta.executequery (SQL);
if (Rs.next ()) {
System.out.println ("Please enter the amount you want to transfer");
Scanner in3 = new Scanner (system.in);
int Z_money = In3.nextint ();
String SQL1 = "Select U_money from Users where u_id= '" +yh+ "";
rs = Sta.executequery (SQL1);
int money = 0;
while (Rs.next ()) {
Money = Rs.getint (1);
}
if (Money >= Z_money) {
String sql2 = "Update users set U_money = u_money-'" +z_money+ "' Where u_id= '" +yh+ "'";
Sta.executeupdate (SQL2);
String sql3 = "Update users set U_money = u_money+ '" +z_money+ "' Where u_id= '" +zh1+ "'";
Sta.executeupdate (SQL3);
System.out.println ("successful transfer");
}else {
System.out.println ("Your balance is insufficient");
}
}else {
System.out.println ("The account you entered does not exist");
}
}else {
System.out.println ("The account you entered is inconsistent, please re-enter");
}
}else if (flag = = 6) {
SYSTEM.OUT.PRINTLN ("Thank you for using!" ");
System.exit (0);
}else {
SYSTEM.OUT.PRINTLN ("Not open this service, please look forward!" ");
}
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
if (rs! = null) {
Rs.close ();
}
if (STA! = null) {
Sta.close ();
}
if (con! = null) {
Con.close ();
}
}catch (SQLException e) {
E.printstacktrace ();
}
}
}
}
Database connection and simple ATM/delete check and change operation