Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. package org.programm.freeman;
  2. import org.programm.Magazine;
  3.  
  4. import java.sql.*;
  5. import java.util.List;
  6.  
  7. /**
  8.  * Created by freeman on 13.10.2015.
  9.  */
  10. public class DB {
  11.  
  12.     private Long id;
  13.     private String group;
  14.     private Integer countStudent;
  15.     private String classroomTeacher;
  16.     private String edit;
  17.     private Boolean allSigned;
  18.  
  19.     // JDBC URL, username and password of MySQL server
  20.     private static final String url = "jdbc:mysql://localhost:3306/atcpcondb";
  21.     private static final String user = "root";
  22.     private static final String password = "root";
  23.  
  24.     // JDBC variables for opening and managing connection
  25.     private static Connection con;
  26.     private static Statement stmt;
  27.     private static ResultSet rs;
  28.  
  29.     public void connectBD()
  30.     {
  31.         try {
  32.             // opening database connection to MySQL server
  33.             con = DriverManager.getConnection(url, user, password);
  34.  
  35.             // getting Statement object to execute query
  36.             stmt = con.createStatement();
  37.  
  38.         } catch (SQLException sqlEx) {
  39.             sqlEx.printStackTrace();
  40.         } finally {
  41.             //close connection ,stmt and resultset here
  42.             try {
  43.                 con.close();
  44.             } catch (SQLException se) { /*can't do anything */ }
  45.             try {
  46.                 stmt.close();
  47.             } catch (SQLException se) { /*can't do anything */ }
  48.             try {
  49.                 rs.close();
  50.             } catch (SQLException se) { /*can't do anything */ }
  51.  
  52.         }
  53.     }
  54.  
  55.  
  56.     public void addAll(List<Magazine> list){
  57.  
  58.         for(int i=0; i<list.size();i++){
  59.           id = list.get(i).getId();
  60.           group = list.get(i).getGroup();
  61.           countStudent = list.get(i).getCountStudent();
  62.           classroomTeacher = list.get(i).getClassroomTeacher();
  63.           edit = list.get(i).getEdit();
  64.           allSigned = list.get(i).getAllSigned();
  65.  
  66.  
  67.             String query = "INSERT INTO atcpcondb.magazine (id, group, countStudents, classroomTeacher, edit, allSigned) \n" +
  68.                     " VALUES ("+id+","+group+","+countStudent+", "+classroomTeacher+","+edit+","+allSigned+");";
  69.  
  70.         // executing SELECT query
  71.         try {
  72.             stmt.executeUpdate(query);
  73.         } catch (SQLException e) {
  74.             e.printStackTrace();
  75.         }
  76.  
  77.     }
  78.  
  79.     }
  80.  
  81. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement