By GokiSoft.com|
09:26 23/08/2021|
Java Advanced
[Share Code] Thêm/sửa/xoá thông tin người dùng - lập trình kết nối CSDL mysql - Java 2 nâng cao - C2010G
https://mvnrepository.com/artifact/mysql/mysql-connector-java
B1. Tao project
B2. Tai thu vien mysql jdbc driver maven
B3. Add thu vien vao project
B4. Chuan bi database & tables
B5. Mapping tables vao trong project du an
tables <-> class object trong java
#Utility.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package java2.lesson07;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Utility {
public static int scanInt(Scanner scan) {
int value;
while(true) {
try {
value = Integer.parseInt(scan.nextLine());
break;
} catch(NumberFormatException e) {
System.out.println("Nhap lai: ");
}
}
return value;
}
}
#Users.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package java2.lesson07;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Users {
String userName, email, password;
public Users() {
}
public Users(String userName, String email, String password) {
this.userName = userName;
this.email = email;
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap username: ");
userName = scan.nextLine();
System.out.println("Nhap email: ");
email = scan.nextLine();
System.out.println("Nhap password: ");
password = scan.nextLine();
}
@Override
public String toString() {
return "Users{" + "userName=" + userName + ", email=" + email + ", password=" + password + '}';
}
}
#UserModify.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package java2.lesson07;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Diep.Tran
*/
public class UserModify {
/**
* Insert, Update, Delete
* @param user
*/
public static void insert(Users user) {
Connection con = null;
PreparedStatement statement = null;
try {
//Mo ket noi toi database
con = DriverManager.getConnection(Config.DB_URL, Config.USERNAME, Config.PASSWORD);
//Thuc them du lieu vao database
String sql = "insert into users(username, email, password) values (?, ?, ?)";
statement = con.prepareStatement(sql);
statement.setString(1, user.getUserName());
statement.setString(2, user.getEmail());
statement.setString(3, user.getPassword());
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//Dong ket noi toi database
if(statement != null) {
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(con != null) {
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static void update(Users user) {
Connection con = null;
PreparedStatement statement = null;
try {
//Mo ket noi toi database
con = DriverManager.getConnection(Config.DB_URL, Config.USERNAME, Config.PASSWORD);
//Thuc them du lieu vao database
String sql = "update users set email = ?, password = ? where username = ?";
statement = con.prepareStatement(sql);
statement.setString(1, user.getEmail());
statement.setString(2, user.getPassword());
statement.setString(3, user.getUserName());
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//Dong ket noi toi database
if(statement != null) {
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(con != null) {
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static void delete(String username) {
Connection con = null;
PreparedStatement statement = null;
try {
//Mo ket noi toi database
con = DriverManager.getConnection(Config.DB_URL, Config.USERNAME, Config.PASSWORD);
//Thuc them du lieu vao database
String sql = "delete from users where username = ?";
statement = con.prepareStatement(sql);
statement.setString(1, username);
statement.execute();
} catch (SQLException ex) {
Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//Dong ket noi toi database
if(statement != null) {
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(con != null) {
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
public static List<Users> getUserList() {
List<Users> userList = new ArrayList<>();
Connection con = null;
PreparedStatement statement = null;
try {
//Mo ket noi toi database
con = DriverManager.getConnection(Config.DB_URL, Config.USERNAME, Config.PASSWORD);
//Thuc them du lieu vao database
String sql = "select * from users";
statement = con.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
//Doc du lieu ra
while(resultSet.next()) {
userList.add(new Users(
resultSet.getString("username"),
resultSet.getString("email"),
resultSet.getString("password")
));
}
} catch (SQLException ex) {
Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
} finally {
//Dong ket noi toi database
if(statement != null) {
try {
statement.close();
} catch (SQLException ex) {
Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(con != null) {
try {
con.close();
} catch (SQLException ex) {
Logger.getLogger(UserModify.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
return userList;
}
}
#Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package java2.lesson07;
import java.util.List;
import java.util.Scanner;
/**
*
* @author Diep.Tran
*/
public class Main {
public static void main(String[] args) {
Users users = new Users();
Scanner scan = new Scanner(System.in);
int choose;
do {
showMenu();
choose = Utility.scanInt(scan);
switch(choose) {
case 1:
users.input();
break;
case 2:
System.out.println(users);
break;
case 3:
//Luu thong tin sinh vien vao database
UserModify.insert(users);
break;
case 4:
List<Users> userList = UserModify.getUserList();
for (Users u : userList) {
System.out.println(u);
}
break;
case 5:
System.out.println("Nhap username can xoa: ");
String username = scan.nextLine();
UserModify.delete(username);
break;
case 6:
System.out.println("Cap nhat thong tin sinh vien: ");
users.input();
UserModify.update(users);
break;
}
} while(choose != 7);
}
static void showMenu() {
System.out.println("1. Nhap thong tin sinh vien");
System.out.println("2. Hien thi thong tin sinh vien");
System.out.println("3. Luu thong tin sv vao database");
System.out.println("4. Doc thong tin sinh vien tu database");
System.out.println("5. Xoa sinh vien khoi database");
System.out.println("6. Cap nhat thong tin sinh vien trong database");
System.out.println("7. Thoat");
System.out.println("Chon: ");
}
}
#Config.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package java2.lesson07;
/**
*
* @author Diep.Tran
*/
public interface Config {
String DB_URL = "jdbc:mysql://localhost:3306/C2010G";
String USERNAME = "root";
String PASSWORD = "";
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)