prateeksharma

oops_jdbc

Dec 8th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4.  
  5. public class connection_db {
  6. public static Connection conn = null;
  7. void connection_db() {
  8. try {
  9. Class.forName("com.mysql.cj.jdbc.Driver");
  10. System.out.println("Connecting to Database....");
  11. String connectionUrl = "jdbc:mysql://localhost:3306/Faculty";
  12. conn = DriverManager.getConnection(connectionUrl,"root","");
  13. System.out.println("Connected to Database Successfully....");
  14. }
  15. catch (SQLException | ClassNotFoundException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. }
  20.  
  21.  
  22.  
  23. public class faculty {
  24. private int id;
  25. private String name;
  26. private int subject;
  27.  
  28. faculty(int id , String name , int subject){
  29. this.id = id;
  30. this.name = name;
  31. this.subject = subject;
  32. }
  33.  
  34. public int getId() {
  35. return id;
  36. }
  37.  
  38. public String getName() {
  39. return name;
  40. }
  41.  
  42. public int getSubject() {
  43. return subject;
  44. }
  45. }
  46.  
  47.  
  48. import java.sql.PreparedStatement;
  49. import java.sql.ResultSet;
  50.  
  51. public class insertRecord extends connection_db {
  52. void insertRecordd(faculty faculty) {
  53. try {
  54. String sql = "INSERT INTO Faculty "
  55. + "VALUES (?,?,?)";
  56. PreparedStatement pst = conn.prepareStatement(sql);
  57. pst.setInt(1, faculty.getId());
  58. pst.setString(2, faculty.getName());
  59. pst.setInt(3, faculty.getSubject());
  60. pst.executeUpdate();
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. }
  66.  
  67.  
  68.  
  69. import java.sql.PreparedStatement;
  70. import java.sql.ResultSet;
  71. import java.util.*;
  72.  
  73. public class send_to_arraylist extends connection_db{
  74. void send_to_arraylist() {
  75. int id = 0;
  76. String name = "";
  77. int subject = 0;
  78. ArrayList<faculty> al =new ArrayList<faculty>();
  79. try {
  80. String sql = "SELECT * FROM Faculty";
  81. PreparedStatement pst = conn.prepareStatement(sql);
  82. ResultSet rs = pst.executeQuery();
  83.  
  84. while (rs.next()) {
  85. id = rs.getInt(1);
  86. name = rs.getString(2);
  87. subject = rs.getInt(3);
  88. faculty f = new faculty(id, name, subject);
  89. al.add(f);
  90.  
  91. Iterator itr = al.iterator();
  92.  
  93. for (int i = 0; i < 1; i++) {
  94. faculty fa = (faculty) itr.next();
  95. System.out.println(id + " " + name + " " + subject);
  96. }
  97. }
  98. }
  99. catch (Exception e){
  100. e.printStackTrace();
  101. }
  102. }
  103. }
  104.  
  105.  
  106.  
  107. import java.util.*;
  108.  
  109. public class jdbc_main {
  110. public static void main(String[] args) {
  111. int id = 0;
  112. String name = "";
  113. int subject = 0;
  114. connection_db conn = new connection_db();
  115. conn.connection_db();
  116. insertRecord ir =new insertRecord();
  117. Scanner sc = new Scanner(System.in);
  118. System.out.println("Insert Records.....");
  119. for (int i = 0; i < 3; i++) {
  120. id = sc.nextInt();
  121. name = sc.next();
  122. subject = sc.nextInt();
  123.  
  124. faculty fa = new faculty(id, name, subject);
  125. ir.insertRecordd(fa);
  126. }
  127. send_to_arraylist ar= new send_to_arraylist();
  128. ar.send_to_arraylist();
  129. }
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. jdbc_employee
  142.  
  143.  
  144. import java.sql.Connection;
  145. import java.sql.DriverManager;
  146. import java.sql.SQLException;
  147.  
  148. public class emp_connection {
  149. public static Connection conn = null;
  150. void connection(){
  151. try {
  152. Class.forName("com.mysql.cj.jdbc.Driver");
  153. System.out.println("Connecting to database....");
  154. String connectionUrl = "jdbc:mysql://localhost:3306/jdbc_emp";
  155. conn = DriverManager.getConnection(connectionUrl,"root","");
  156. System.out.println("Connected to database successfully....");
  157. }
  158. catch (SQLException | ClassNotFoundException e) {
  159. e.printStackTrace();
  160. }
  161. }
  162. }
  163.  
  164.  
  165.  
  166. public class employee {
  167. private int id;
  168. private String name;
  169. private int salary;
  170.  
  171. employee(int id , String name , int salary) {
  172. this.id = id;
  173. this.name = name;
  174. this.salary = salary;
  175. }
  176.  
  177. public int getId() {
  178. return id;
  179. }
  180.  
  181. public String getName() {
  182. return name;
  183. }
  184.  
  185. public int getSalary() {
  186. return salary;
  187. }
  188. }
  189.  
  190.  
  191.  
  192. import java.sql.*;
  193.  
  194. public class emp_insertRecord extends emp_connection{
  195. int result = 0;
  196. int insertRecord(employee emp){
  197. try {
  198. String sql = "INSERT INTO employee "
  199. + "(id,name,salary)" + "VALUES (?,?,?)";
  200. PreparedStatement pst = conn.prepareStatement(sql);
  201. pst.setInt(1,emp.getId());
  202. pst.setString(2,emp.getName());
  203. pst.setInt(3,emp.getSalary());
  204. result = pst.executeUpdate();
  205. }
  206. catch (SQLException e) {
  207. e.printStackTrace();
  208. }
  209. return result;
  210. }
  211.  
  212. void displayRecord(){
  213. try {
  214. String sql = "SELECT * FROM employee";
  215. PreparedStatement pst = conn.prepareStatement(sql);
  216. ResultSet rs = pst.executeQuery();
  217.  
  218. while(rs.next()){
  219. System.out.print(" " + rs.getInt(1));
  220. System.out.print(" " + rs.getString(2));
  221. System.out.println(" " + rs.getInt(3));
  222. }
  223. }
  224. catch (Exception e) {
  225. e.printStackTrace();
  226. }
  227. }
  228.  
  229. }
  230.  
  231.  
  232.  
  233. import java.util.Scanner;
  234.  
  235. public class emp_main extends emp_connection{
  236. public static void main(String[] args) {
  237. int id = 0;
  238. String name = "";
  239. int salary = 0;
  240. emp_connection con = new emp_connection();
  241. con.connection();
  242. emp_insertRecord e =new emp_insertRecord();
  243. Scanner sc = new Scanner(System.in);
  244. System.out.println("Insert Records.....");
  245. for (int i = 0; i < 1; i++) {
  246. id = sc.nextInt();
  247. name = sc.next();
  248. salary = sc.nextInt();
  249.  
  250. employee emp = new employee(id, name, salary);
  251. e.insertRecord(emp);
  252. }
  253. System.out.println("Records inserted....");
  254. System.out.println("Displaying Records....");
  255. e.displayRecord();
  256.  
  257. }
  258. }
Add Comment
Please, Sign In to add comment