Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class connection_db {
- public static Connection conn = null;
- void connection_db() {
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- System.out.println("Connecting to Database....");
- String connectionUrl = "jdbc:mysql://localhost:3306/Faculty";
- conn = DriverManager.getConnection(connectionUrl,"root","");
- System.out.println("Connected to Database Successfully....");
- }
- catch (SQLException | ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
- public class faculty {
- private int id;
- private String name;
- private int subject;
- faculty(int id , String name , int subject){
- this.id = id;
- this.name = name;
- this.subject = subject;
- }
- public int getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public int getSubject() {
- return subject;
- }
- }
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- public class insertRecord extends connection_db {
- void insertRecordd(faculty faculty) {
- try {
- String sql = "INSERT INTO Faculty "
- + "VALUES (?,?,?)";
- PreparedStatement pst = conn.prepareStatement(sql);
- pst.setInt(1, faculty.getId());
- pst.setString(2, faculty.getName());
- pst.setInt(3, faculty.getSubject());
- pst.executeUpdate();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.*;
- public class send_to_arraylist extends connection_db{
- void send_to_arraylist() {
- int id = 0;
- String name = "";
- int subject = 0;
- ArrayList<faculty> al =new ArrayList<faculty>();
- try {
- String sql = "SELECT * FROM Faculty";
- PreparedStatement pst = conn.prepareStatement(sql);
- ResultSet rs = pst.executeQuery();
- while (rs.next()) {
- id = rs.getInt(1);
- name = rs.getString(2);
- subject = rs.getInt(3);
- faculty f = new faculty(id, name, subject);
- al.add(f);
- Iterator itr = al.iterator();
- for (int i = 0; i < 1; i++) {
- faculty fa = (faculty) itr.next();
- System.out.println(id + " " + name + " " + subject);
- }
- }
- }
- catch (Exception e){
- e.printStackTrace();
- }
- }
- }
- import java.util.*;
- public class jdbc_main {
- public static void main(String[] args) {
- int id = 0;
- String name = "";
- int subject = 0;
- connection_db conn = new connection_db();
- conn.connection_db();
- insertRecord ir =new insertRecord();
- Scanner sc = new Scanner(System.in);
- System.out.println("Insert Records.....");
- for (int i = 0; i < 3; i++) {
- id = sc.nextInt();
- name = sc.next();
- subject = sc.nextInt();
- faculty fa = new faculty(id, name, subject);
- ir.insertRecordd(fa);
- }
- send_to_arraylist ar= new send_to_arraylist();
- ar.send_to_arraylist();
- }
- }
- jdbc_employee
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class emp_connection {
- public static Connection conn = null;
- void connection(){
- try {
- Class.forName("com.mysql.cj.jdbc.Driver");
- System.out.println("Connecting to database....");
- String connectionUrl = "jdbc:mysql://localhost:3306/jdbc_emp";
- conn = DriverManager.getConnection(connectionUrl,"root","");
- System.out.println("Connected to database successfully....");
- }
- catch (SQLException | ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
- public class employee {
- private int id;
- private String name;
- private int salary;
- employee(int id , String name , int salary) {
- this.id = id;
- this.name = name;
- this.salary = salary;
- }
- public int getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public int getSalary() {
- return salary;
- }
- }
- import java.sql.*;
- public class emp_insertRecord extends emp_connection{
- int result = 0;
- int insertRecord(employee emp){
- try {
- String sql = "INSERT INTO employee "
- + "(id,name,salary)" + "VALUES (?,?,?)";
- PreparedStatement pst = conn.prepareStatement(sql);
- pst.setInt(1,emp.getId());
- pst.setString(2,emp.getName());
- pst.setInt(3,emp.getSalary());
- result = pst.executeUpdate();
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- return result;
- }
- void displayRecord(){
- try {
- String sql = "SELECT * FROM employee";
- PreparedStatement pst = conn.prepareStatement(sql);
- ResultSet rs = pst.executeQuery();
- while(rs.next()){
- System.out.print(" " + rs.getInt(1));
- System.out.print(" " + rs.getString(2));
- System.out.println(" " + rs.getInt(3));
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- import java.util.Scanner;
- public class emp_main extends emp_connection{
- public static void main(String[] args) {
- int id = 0;
- String name = "";
- int salary = 0;
- emp_connection con = new emp_connection();
- con.connection();
- emp_insertRecord e =new emp_insertRecord();
- Scanner sc = new Scanner(System.in);
- System.out.println("Insert Records.....");
- for (int i = 0; i < 1; i++) {
- id = sc.nextInt();
- name = sc.next();
- salary = sc.nextInt();
- employee emp = new employee(id, name, salary);
- e.insertRecord(emp);
- }
- System.out.println("Records inserted....");
- System.out.println("Displaying Records....");
- e.displayRecord();
- }
- }
Add Comment
Please, Sign In to add comment