Advertisement
nguyenvanquan7826

connect database 2

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