Advertisement
nguyenvanquan7826

connect database

May 3rd, 2014
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.20 KB | None | 0 0
  1. package nguyenvanquan7826.test;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.Scanner;
  10.  
  11. public class MyConnection {
  12.     private Connection connection;
  13.     private final String driver = "com.mysql.jdbc.Driver";
  14.     private final String dataName = "jdbc:mysql://localhost:3306/student";
  15.     private final String user = "root";
  16.     private final String pass = "lamgicopass";
  17.  
  18.     public static void main(String[] args) {
  19.         // create connect
  20.         MyConnection connect = new MyConnection();
  21.         // connect database
  22.         connect.connect();
  23.  
  24.         while (true) {
  25.             connect.showMenu();
  26.         }
  27.     }
  28.  
  29.     // show menu select method
  30.     private void showMenu() {
  31.         String tableName = "student_info";
  32.         String id = "";
  33.         Scanner scan = new Scanner(System.in);
  34.         System.out.println("Select an method: ");
  35.         System.out.println("1: show data in database");
  36.         System.out.println("2: show student by id in database");
  37.         System.out.println("3: delete student by id in database");
  38.         System.out.println("4: update student info by id in database");
  39.         System.out.println("5: insert student into database");
  40.         System.out.println("6: exit");
  41.         int select = Integer.parseInt(scan.nextLine());
  42.         switch (select) {
  43.         case 1:
  44.             showData(getData(tableName));
  45.             break;
  46.  
  47.         case 2:
  48.             System.out.println("Enter id of student you want show:");
  49.             id = scan.nextLine();
  50.             showData(getDataId(tableName, id));
  51.             break;
  52.         case 3:
  53.             System.out.println("Enter id of student you want delete:");
  54.             id = scan.nextLine();
  55.             deleteId(tableName, id);
  56.             break;
  57.         case 4:
  58.             System.out.println("Enter id of student you want update:");
  59.             id = scan.nextLine();
  60.             updateId(tableName, id, createStudent());
  61.             break;
  62.         case 5:
  63.             insertId(tableName, createStudent());
  64.             break;
  65.         case 6:
  66.             System.exit(0);
  67.         }
  68.     }
  69.  
  70.     // connect to database
  71.     private void connect() {
  72.         try {
  73.             Class.forName(driver);
  74.             connection = DriverManager.getConnection(dataName, user, pass);
  75.             System.out.println("Ket noi thanh cong");
  76.             System.out.println("~~~~~~~~~~~~~~~~~~");
  77.         } catch (ClassNotFoundException e) {
  78.         } catch (SQLException e) {
  79.             e.printStackTrace();
  80.         }
  81.     }
  82.  
  83.     // disconnect to database
  84.     private void disconnect() {
  85.         try {
  86.             connection.close();
  87.         } catch (SQLException e) {
  88.             // TODO Auto-generated catch block
  89.             e.printStackTrace();
  90.         }
  91.     }
  92.  
  93.     // show data in resultset - data is selected
  94.     private void showData(ResultSet rs) {
  95.         System.out.printf("%-5s %-20s %-5s\n", "Id", "Name", "Point");
  96.         System.out.printf("--------------------------------\n");
  97.         try {
  98.             while (rs.next()) {
  99.                 System.out.printf("%-5s %-20s %-5.2f\n", rs.getString(1),
  100.                         rs.getString(2), rs.getDouble(3));
  101.             }
  102.         } catch (SQLException e) {
  103.             e.printStackTrace();
  104.         }
  105.         System.out.println("~~~~~~~~~~~~~~~~~~");
  106.     }
  107.  
  108.     // get data from database
  109.     private ResultSet getData(String tableName) {
  110.         ResultSet rs = null;
  111.         String sqlCommand = "select * from " + tableName;
  112.         try {
  113.             Statement statement = connection.createStatement();
  114.             rs = statement.executeQuery(sqlCommand);
  115.         } catch (SQLException e) {
  116.             e.printStackTrace();
  117.         }
  118.         return rs;
  119.     }
  120.  
  121.     // get Student by id
  122.     private ResultSet getDataId(String tableName, String id) {
  123.         ResultSet rs = null;
  124.         String sqlCommand = "select * from " + tableName + " where `id` = ?";
  125.         PreparedStatement ps;
  126.         try {
  127.             ps = connection.prepareStatement(sqlCommand);
  128.             ps.setString(1, id);
  129.             rs = ps.executeQuery();
  130.         } catch (SQLException e) {
  131.             e.printStackTrace();
  132.         }
  133.         return rs;
  134.     }
  135.  
  136.     // delete student by id
  137.     private void deleteId(String tableName, String id) {
  138.         String sqlCommand = "delete from " + tableName + " where `id` = ?";
  139.         PreparedStatement ps;
  140.         try {
  141.             ps = connection.prepareStatement(sqlCommand);
  142.             ps.setString(1, id);
  143.             if (ps.executeUpdate() > 0) {
  144.                 System.out.println("delete success!");
  145.             } else {
  146.                 System.out.println("error delete");
  147.             }
  148.         } catch (SQLException e) {
  149.             System.out.println("error delete");
  150.         }
  151.     }
  152.  
  153.     // update student by id
  154.     private void updateId(String tableName, String id, Student student) {
  155.         String sqlCommand = "update " + tableName
  156.                 + " set `name` = ?, `point` = ? where `id` = ?";
  157.         PreparedStatement ps;
  158.         try {
  159.             ps = connection.prepareStatement(sqlCommand);
  160.             ps.setString(1, student.getName());
  161.             ps.setDouble(2, student.getPoint());
  162.             ps.setString(3, student.getId());
  163.             if (ps.executeUpdate() > 0) {
  164.                 System.out.println("update success!");
  165.             } else {
  166.                 System.out.println("error update 1 ");
  167.             }
  168.         } catch (SQLException e) {
  169.             System.out.println("error update 2" + e.toString());
  170.         }
  171.     }
  172.  
  173.     // insert a student
  174.     private void insertId(String tableName, Student student) {
  175.         String sqlCommand = "insert into " + tableName + " value(?, ?, ?)";
  176.         PreparedStatement ps;
  177.         try {
  178.             ps = connection.prepareStatement(sqlCommand);
  179.             ps.setString(1, student.getId());
  180.             ps.setString(2, student.getName());
  181.             ps.setDouble(3, student.getPoint());
  182.             if (ps.executeUpdate() > 0) {
  183.                 System.out.println("insert success!");
  184.             } else {
  185.                 System.out.println("error insert");
  186.             }
  187.         } catch (SQLException e) {
  188.             System.out.println("error insert");
  189.         }
  190.     }
  191.  
  192.     // create a student
  193.     private Student createStudent() {
  194.         Scanner scan = new Scanner(System.in);
  195.         Student student = null;
  196.         System.out.println("Enter id, name and point of student");
  197.         String id = scan.nextLine();
  198.         String name = scan.nextLine();
  199.         double point = Double.parseDouble(scan.nextLine());
  200.         student = new Student(id, name, point);
  201.         return student;
  202.     }
  203.  
  204. }
  205.  
  206. class Student {
  207.     private String id;
  208.     private String name;
  209.     private double point;
  210.  
  211.     public Student(String id, String name, double point) {
  212.         super();
  213.         this.id = id;
  214.         this.name = name;
  215.         this.point = point;
  216.     }
  217.  
  218.     public String getId() {
  219.         return id;
  220.     }
  221.  
  222.     public void setId(String id) {
  223.         this.id = id;
  224.     }
  225.  
  226.     public String getName() {
  227.         return name;
  228.     }
  229.  
  230.     public void setName(String name) {
  231.         this.name = name;
  232.     }
  233.  
  234.     public double getPoint() {
  235.         return point;
  236.     }
  237.  
  238.     public void setPoint(double point) {
  239.         this.point = point;
  240.     }
  241.  
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement