Java and MySQL database connection, and implementation of ADD, delete, modify, query

Source: Internet
Author: User

UserAccount class:

public class UserAccount {
Private String account;//Account card number
Private String uname;//Name
Private String upwd;//Password
private double balance;//Balance

//Construction method
Public UserAccount (String account, String uname, String upwd, double balance) {
Super ();
This.account = account;
This.uname = uname;
This.upwd = upwd;
This.balance = balance;
}
Public String Getaccount () {
return account;
}
public void Setaccount (String account) {
This.account = account;
}
Public String Getuname () {
return uname;
}
public void Setuname (String uname) {
This.uname = uname;
}
Public String getupwd () {
return upwd;
}
public void Setupwd (String upwd) {
This.upwd = upwd;
}
Public double getbalance () {
return balance;
}
public void setbalance (double balance) {
This.balance = balance;
}

import java.sql.Connection;
import Java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import Java.util.Calendar;
import java.util.List;
import Java.util.Scanner;

public class Mysqlconnect {
private static final String driver= "Com.mysql.jdbc.Driver";
private static final String url= "Jdbc:mysql://localhost:3306/bank";
private static final String user= "root";
private static final String pwd= "root";
Static Connection Conn=null; //Link Object
Static Statement St=null; //Operation
Static PreparedStatement Ps=null;
Static ResultSet Rs=null; //Result set


Connecting Database encapsulation
public static void Getconnection () {
try {
Class.forName (DRIVER);
Conn =drivermanager.getconnection (URL, USER, PWD);
} catch (ClassNotFoundException e) {
E.printstacktrace ();
} catch (SQLException e) {
E.printstacktrace ();
}
}


Close Resource
public static void CloseAll () {
try {
if (rs!=null)
Rs.close ();
if (st!=null)
St.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
}


//Querying data in all databases
public static list<useraccount>getallusers () {
List<useraccount>list=new arraylist<useraccount> ();
try {
   //Get Connected
Getconnection ();
   //Build SQL statements
String sql = "Select account,uname,upassword,balance from UserAccount";
//Create statement Object
st = Conn.createstatement ();
   //Execute SQL statement
Rs=st.executequery (SQL);
//Processing result set
while (Rs.next ()) {
UserAccount user=new UserAccount (rs.getstring (1), rs.getstring (2), rs.getstring (3), rs.getdouble (4));
List.add (user);
}
} catch (SQLException e) {
E.printstacktrace ();
}finally{
CloseAll ();
}
return list;
}
//delete data from the database (directly deleted via account)
public static int Deleteuseraccount (String user) {
try {
   //Get Connected
Getconnection ();
   //Build SQL statements
String sql= "Delete from UserAccount where account=?";
   //Build stated Objects
Ps=conn.preparestatement (SQL);
Ps.setstring (1, user);
return Ps.executeupdate ();
}catch (SQLException e) {
E.printstacktrace ();
}finally{
CloseAll ();
}
return-1;
}
//Increase data in the database
public static int Adduseraccount (UserAccount user) {
try {
//Get Connected
Getconnection ();
//Build SQL statements
String sql= "INSERT into UserAccount (account,uname,upassword,balance) VALUES (?,?,?,?)";
   //Build stated Objects
Ps=conn.preparestatement (SQL);
Ps.setstring (1, User.getaccount ());
Ps.setstring (2, User.getuname ());
Ps.setstring (3, User.getupwd ());
Ps.setdouble (4, User.getbalance ());
return Ps.executeupdate ();
}catch (SQLException e) {
E.printstacktrace ();
}finally{
CloseAll ();
}
return-1;
}
//Modify data in the database
public static int updateuserbypstmt (UserAccount user) {
try {
   //Get Connected
Getconnection ();
   //Build SQL statements
String sql= "Update useraccount set uname=?,upassword=? where account=? ";
   //Build stated Objects
Ps=conn.preparestatement (SQL);
Ps.setstring (1, User.getuname ());
Ps.setstring (2, User.getupwd ());
Ps.setstring (3, User.getaccount ());
return Ps.executeupdate ();
}catch (SQLException e) {
E.printstacktrace ();
}finally{
CloseAll ();
}
return-1;
}

Check MySQL database single data by account
public static UserAccount Getuserbyaccount (String account) {
UserAccount User=null;
try{
Getconnection ();
String sql= "Select Account,uname,upassword,balance from UserAccount where account= '" +account+ "'";
St=conn.createstatement ();
Rs=st.executequery (SQL);
if (Rs.next ()) {
User=new UserAccount (rs.getstring (1), rs.getstring (2), rs.getstring (3), rs.getdouble (4));
}
}catch (Exception e) {
}finally{
CloseAll ();
}
return user;
}

Java and MySQL database connection, and implementation of ADD, delete, modify, query

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.