Advertisement
nguyenvanquan7826

connect database - class MyConnect

May 9th, 2014
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. package nguyenvanquan7826.database;
  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.  
  10. public class MyConnect {
  11.  
  12.     private final String className = "com.mysql.jdbc.Driver";
  13.     private final String url = "jdbc:mysql://localhost:3306/student";
  14.     private final String user = "root";
  15.     private final String pass = "lamgicopass";
  16.  
  17.     private String table = "student_info";
  18.  
  19.     private Connection connection;
  20.  
  21.     // connect to database
  22.     public void connect() {
  23.         try {
  24.             Class.forName(className);
  25.             connection = DriverManager.getConnection(url, user, pass);
  26.             System.out.println("Connect success!");
  27.         } catch (ClassNotFoundException e) {
  28.             System.out.println("Class not found!");
  29.         } catch (SQLException e) {
  30.             System.out.println("Error connection!");
  31.         }
  32.     }
  33.  
  34.     // show data is selected
  35.     public void showData(ResultSet rs) {
  36.         try {
  37.             while (rs.next()) {
  38.                 System.out.printf("%-10s %-20s %-5.2f \n", rs.getString(1),
  39.                         rs.getString(2), rs.getDouble(3));
  40.             }
  41.         } catch (SQLException e) {
  42.         }
  43.     }
  44.  
  45.     // get data and return Resultset with command select
  46.     public ResultSet getData() {
  47.         ResultSet rs = null;
  48.         String sqlCommand = "select * from " + table;
  49.         Statement st;
  50.         try {
  51.             st = connection.createStatement();
  52.             rs = st.executeQuery(sqlCommand);
  53.         } catch (SQLException e) {
  54.             System.out.println("select error \n" + e.toString());
  55.         }
  56.         return rs;
  57.     }
  58.  
  59.     // get data and return Resultset with command select by Id
  60.     public ResultSet getDataId(String id) {
  61.         ResultSet rs = null;
  62.         String sqlCommand = "select * from " + table + " where id = ?";
  63.         PreparedStatement pst = null;
  64.         try {
  65.             pst = connection.prepareStatement(sqlCommand);
  66.             // replace "?" by id
  67.             pst.setString(1, id);
  68.             rs = pst.executeQuery();
  69.         } catch (SQLException e) {
  70.             System.out.println("select error \n" + e.toString());
  71.         }
  72.         return rs;
  73.     }
  74.  
  75.     // delete by Id
  76.     public void deleteId(String id) {
  77.         String sqlCommand = "delete from " + table + " where id = ?";
  78.         PreparedStatement pst = null;
  79.         try {
  80.             pst = connection.prepareStatement(sqlCommand);
  81.             pst.setString(1, id);
  82.             if (pst.executeUpdate() > 0) {
  83.                 System.out.println("delete success");
  84.             } else {
  85.                 System.out.println("delete error \n");
  86.             }
  87.         } catch (SQLException e) {
  88.             System.out.println("delete error \n" + e.toString());
  89.         }
  90.     }
  91.  
  92.     // insert student s to database
  93.     public void insert(Student s) {
  94.         String sqlCommand = "insert into " + table + " value(?, ?, ?)";
  95.         PreparedStatement pst = null;
  96.         try {
  97.             pst = connection.prepareStatement(sqlCommand);
  98.             // replace three "?" by id, Name and point of Studnet s
  99.             pst.setString(1, s.getId());
  100.             pst.setString(2, s.getName());
  101.             pst.setDouble(3, s.getPoint());
  102.             if (pst.executeUpdate() > 0) {
  103.                 System.out.println("insert success");
  104.             } else {
  105.                 System.out.println("insert error \n");
  106.             }
  107.         } catch (SQLException e) {
  108.             System.out.println("insert error \n" + e.toString());
  109.         }
  110.     }
  111.  
  112.     // update studetn by Id
  113.     public void updateId(String id, Student s) {
  114.         String sqlCommand = "update " + table
  115.                 + " set name = ?, point = ? where id = ?";
  116.         PreparedStatement pst = null;
  117.         try {
  118.             pst = connection.prepareStatement(sqlCommand);
  119.             pst.setString(1, s.getName());
  120.             pst.setDouble(2, s.getPoint());
  121.             pst.setString(3, s.getId());
  122.             if (pst.executeUpdate() > 0) {
  123.                 System.out.println("update success");
  124.             } else {
  125.                 System.out.println("update error \n");
  126.             }
  127.         } catch (SQLException e) {
  128.             System.out.println("update error \n" + e.toString());
  129.         }
  130.     }
  131.  
  132.     public static void main(String[] args) {
  133.         MyConnect myConnect = new MyConnect();
  134.         myConnect.connect();
  135.         // myConnect.showData(myConnect.getDataId("DTC1"));
  136.         // myConnect.deleteId("DTC5");
  137.         myConnect.updateId("DTC2", new Student("DTC2", "Vu Cong Tinh", 9.0));
  138.         myConnect.showData(myConnect.getData());
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement