Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package nguyenvanquan7826.test;
- 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.Scanner;
- public class MyConnection {
- private Connection connection;
- private final String driver = "com.mysql.jdbc.Driver";
- private final String dataName = "jdbc:mysql://localhost:3306/student";
- private final String user = "root";
- private final String pass = "lamgicopass";
- public static void main(String[] args) {
- // create connect
- MyConnection connect = new MyConnection();
- // connect database
- connect.connect();
- while (true) {
- connect.showMenu();
- }
- }
- // show menu select method
- private void showMenu() {
- String tableName = "student_info";
- String id = "";
- Scanner scan = new Scanner(System.in);
- System.out.println("Select an method: ");
- System.out.println("1: show data in database");
- System.out.println("2: show student by id in database");
- System.out.println("3: delete student by id in database");
- System.out.println("4: update student info by id in database");
- System.out.println("5: insert student into database");
- System.out.println("6: exit");
- int select = Integer.parseInt(scan.nextLine());
- switch (select) {
- case 1:
- showData(getData(tableName));
- break;
- case 2:
- System.out.println("Enter id of student you want show:");
- id = scan.nextLine();
- showData(getDataId(tableName, id));
- break;
- case 3:
- System.out.println("Enter id of student you want delete:");
- id = scan.nextLine();
- deleteId(tableName, id);
- break;
- case 4:
- System.out.println("Enter id of student you want update:");
- id = scan.nextLine();
- updateId(tableName, id, createStudent());
- break;
- case 5:
- insertId(tableName, createStudent());
- break;
- case 6:
- System.exit(0);
- }
- }
- // connect to database
- private void connect() {
- try {
- Class.forName(driver);
- connection = DriverManager.getConnection(dataName, user, pass);
- System.out.println("Ket noi thanh cong");
- System.out.println("~~~~~~~~~~~~~~~~~~");
- } catch (ClassNotFoundException e) {
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- // disconnect to database
- private void disconnect() {
- try {
- connection.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- // show data in resultset - data is selected
- private void showData(ResultSet rs) {
- System.out.printf("%-5s %-20s %-5s\n", "Id", "Name", "Point");
- System.out.printf("--------------------------------\n");
- try {
- while (rs.next()) {
- System.out.printf("%-5s %-20s %-5.2f\n", rs.getString(1),
- rs.getString(2), rs.getDouble(3));
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- System.out.println("~~~~~~~~~~~~~~~~~~");
- }
- // get data from database
- private ResultSet getData(String tableName) {
- ResultSet rs = null;
- String sqlCommand = "select * from " + tableName;
- try {
- Statement statement = connection.createStatement();
- rs = statement.executeQuery(sqlCommand);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return rs;
- }
- // get Student by id
- private ResultSet getDataId(String tableName, String id) {
- ResultSet rs = null;
- String sqlCommand = "select * from " + tableName + " where `id` = ?";
- PreparedStatement ps;
- try {
- ps = connection.prepareStatement(sqlCommand);
- ps.setString(1, id);
- rs = ps.executeQuery();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return rs;
- }
- // delete student by id
- private void deleteId(String tableName, String id) {
- String sqlCommand = "delete from " + tableName + " where `id` = ?";
- PreparedStatement ps;
- try {
- ps = connection.prepareStatement(sqlCommand);
- ps.setString(1, id);
- if (ps.executeUpdate() > 0) {
- System.out.println("delete success!");
- } else {
- System.out.println("error delete");
- }
- } catch (SQLException e) {
- System.out.println("error delete");
- }
- }
- // update student by id
- private void updateId(String tableName, String id, Student student) {
- String sqlCommand = "update " + tableName
- + " set `name` = ?, `point` = ? where `id` = ?";
- PreparedStatement ps;
- try {
- ps = connection.prepareStatement(sqlCommand);
- ps.setString(1, student.getName());
- ps.setDouble(2, student.getPoint());
- ps.setString(3, student.getId());
- if (ps.executeUpdate() > 0) {
- System.out.println("update success!");
- } else {
- System.out.println("error update 1 ");
- }
- } catch (SQLException e) {
- System.out.println("error update 2" + e.toString());
- }
- }
- // insert a student
- private void insertId(String tableName, Student student) {
- String sqlCommand = "insert into " + tableName + " value(?, ?, ?)";
- PreparedStatement ps;
- try {
- ps = connection.prepareStatement(sqlCommand);
- ps.setString(1, student.getId());
- ps.setString(2, student.getName());
- ps.setDouble(3, student.getPoint());
- if (ps.executeUpdate() > 0) {
- System.out.println("insert success!");
- } else {
- System.out.println("error insert");
- }
- } catch (SQLException e) {
- System.out.println("error insert");
- }
- }
- // create a student
- private Student createStudent() {
- Scanner scan = new Scanner(System.in);
- Student student = null;
- System.out.println("Enter id, name and point of student");
- String id = scan.nextLine();
- String name = scan.nextLine();
- double point = Double.parseDouble(scan.nextLine());
- student = new Student(id, name, point);
- return student;
- }
- }
- class Student {
- private String id;
- private String name;
- private double point;
- public Student(String id, String name, double point) {
- super();
- this.id = id;
- this.name = name;
- this.point = point;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public double getPoint() {
- return point;
- }
- public void setPoint(double point) {
- this.point = point;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement