Advertisement
techcws

Ass1-2

Oct 5th, 2023
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.53 KB | None | 0 0
  1. Name:-
  2. Subject:- IPWCT
  3. Enrollment-No:-
  4.  
  5. /*
  6.  * To change this license header, choose License Headers in Project Properties.
  7.  * To change this template file, choose Tools | Templates
  8.  * and open the template in the editor.
  9.  */
  10. package Srki_r_jdbc;
  11.  import java.sql.*;
  12. import java.io.*;
  13. /**
  14.  *
  15.  * @author srki-078
  16.  */
  17. public class srki_Jdbc {
  18.  
  19.  
  20.     /**
  21.      * @param args the command line arguments
  22.      */
  23.     public static void main(String[] args) {
  24.         // TODO code application logic here
  25.        
  26.         Connection con = null;
  27.       System.out.println("connection is null");
  28.       DataInputStream dis = new DataInputStream(System.in);
  29.         try {
  30.             int id;
  31.             String emp_name;
  32.             Float salary;
  33.             String Designation;
  34.             String user = "root";
  35.             String pass = "";
  36.             String localhost = "jdbc:mysql://localhost:3310/Employee_det";
  37. //            Class.forName("com.mysql.jdbc.Driver");
  38.             Class.forName("com.mysql.jdbc.Driver");
  39.             con = DriverManager.getConnection(localhost, user, pass);
  40.             if(con!=null){
  41.                 System.out.println("Connected successfully");
  42.            
  43. //      
  44. //                    System.out.println("New User Inserted");
  45. //                }
  46. //                      
  47. //          
  48. //                
  49. //                System.out.println("Enter emp_name");
  50. //                emp_name = dis.readLine();
  51. //                
  52. //                System.out.println("Enter salary");
  53. //                salary = Float.parseFloat(dis.readLine());
  54. //                
  55. //                System.out.println("enter Designation");
  56. //                Designation = dis.readLine();
  57. //                
  58. //                String sql ="INSERT INTO Emp_Info(pname,salary,designation) VALUES(?,?,?)";
  59. //                PreparedStatement Statement=con.prepareStatement((sql));
  60. //                Statement.setString(1, emp_name);
  61. //                Statement.setFloat(2,salary);
  62. //                Statement.setString(3,Designation);
  63. //                
  64. //                int rowsInserted = Statement.executeUpdate();
  65. //                if(rowsInserted>0){
  66. //                  Statement stmt = con.createStatement();
  67.  
  68. //                  Statement stmt = con.createStatement();
  69. //                  String sql ="select * from Emp_Info";
  70. //                  ResultSet rs = stmt.executeQuery(sql);
  71. //            
  72. //            
  73. //            while (rs.next()){
  74. //                System.out.println(rs.getInt("emp_id") + " " + rs.getString("emp_name"));
  75. //            }
  76. //            con.close();
  77. //            
  78. //            }
  79. //            else{
  80. //                System.out.println("can't connected");
  81. // UPDATE CODE
  82.  
  83.              System.out.println("enter ID");
  84.      id=Integer.parseInt(dis.readLine());
  85.                 System.out.println("enter Name");
  86.               emp_name=dis.readLine();
  87.                 String sql ="UPDATE Emp_Info SET emp_name=? WHERE emp_id=?";
  88.                
  89.                 PreparedStatement statement = con.prepareStatement(sql);
  90.                 statement.setString(1,emp_name);
  91.                statement.setInt(2, id);
  92.                
  93.                 int rowUpdated=statement.executeUpdate();
  94.                if(rowUpdated>0){
  95.                     System.out.println("An existing user was updated successfully");
  96.                 }
  97.                
  98.            // DELETE CODE
  99.       //     String sql = "DELETE from Emp_Info where emp_id=?";
  100.         //        System.out.println("enter emp_id");
  101.           //      id = Integer.parseInt(dis.readLine());
  102.                
  103.             //    PreparedStatement Statement = con.prepareStatement(sql);
  104.               //  Statement.setInt(1, id);
  105.                
  106.             //    int rowDeleted=Statement.executeUpdate();
  107.                
  108.               //  if(rowDeleted>0){
  109.                 //    System.out.println("A USER wa DELETED successfully");
  110.               //  }
  111.                
  112.                
  113.            }
  114.            
  115.         } catch (Exception e) {
  116.             System.out.println(e.getMessage());
  117.         }
  118.     }  
  119. }
  120.  
  121.         package harsh_r_jdbc;
  122.  import java.sql.*;
  123. import java.io.*;
  124. /**
  125.  *
  126.  * @author srki-078
  127.  */
  128. public class harsh_Jdbc {
  129.  
  130.  
  131.     /**
  132.      * @param args the command line arguments
  133.      */
  134.     public static void main(String[] args) {
  135.         // TODO code application logic here
  136.        
  137.         Connection con = null;
  138.       System.out.println("connection is null");
  139.       DataInputStream dis = new DataInputStream(System.in);
  140.         try {
  141.             int id;
  142.             String emp_name;
  143.             Float salary;
  144.             String Designation;
  145.             String user = "root";
  146.             String pass = "";
  147.             String localhost = "jdbc:mysql://localhost:3310/Employee_det";
  148. //            Class.forName("com.mysql.jdbc.Driver");
  149.             Class.forName("com.mysql.jdbc.Driver");
  150.             con = DriverManager.getConnection(localhost, user, pass);
  151.             if(con!=null){
  152.                 System.out.println("Connected successfully");
  153.            
  154. //      
  155. //                    System.out.println("New User Inserted");
  156. //                }
  157. //                      
  158. //          
  159. //                
  160. //                System.out.println("Enter emp_name");
  161. //                emp_name = dis.readLine();
  162. //                
  163. //                System.out.println("Enter salary");
  164. //                salary = Float.parseFloat(dis.readLine());
  165. //                
  166. //                System.out.println("enter Designation");
  167. //                Designation = dis.readLine();
  168. //                
  169. //                String sql ="INSERT INTO Emp_Info(pname,salary,designation) VALUES(?,?,?)";
  170. //                PreparedStatement Statement=con.prepareStatement((sql));
  171. //                Statement.setString(1, emp_name);
  172. //                Statement.setFloat(2,salary);
  173. //                Statement.setString(3,Designation);
  174. //                
  175. //                int rowsInserted = Statement.executeUpdate();
  176. //                if(rowsInserted>0){
  177. //                  Statement stmt = con.createStatement();
  178.  
  179. //                  Statement stmt = con.createStatement();
  180. //                  String sql ="select * from Emp_Info";
  181. //                  ResultSet rs = stmt.executeQuery(sql);
  182. //            
  183. //            
  184. //            while (rs.next()){
  185. //                System.out.println(rs.getInt("emp_id") + " " + rs.getString("emp_name"));
  186. //            }
  187. //            con.close();
  188. //            
  189. //            }
  190. //            else{
  191. //                System.out.println("can't connected");
  192. // UPDATE CODE
  193.  
  194.              System.out.println("enter ID");
  195.      id=Integer.parseInt(dis.readLine());
  196.                 System.out.println("enter Name");
  197.               emp_name=dis.readLine();
  198.                 String sql ="UPDATE Emp_Info SET emp_name=? WHERE emp_id=?";
  199.                
  200.                 PreparedStatement statement = con.prepareStatement(sql);
  201.                 statement.setString(1,emp_name);
  202.                statement.setInt(2, id);
  203.                
  204.                 int rowUpdated=statement.executeUpdate();
  205.                if(rowUpdated>0){
  206.                     System.out.println("An existing user was updated successfully");
  207.                 }
  208.                
  209.            // DELETE CODE
  210.       //     String sql = "DELETE from Emp_Info where emp_id=?";
  211.         //        System.out.println("enter emp_id");
  212.           //      id = Integer.parseInt(dis.readLine());
  213.                
  214.             //    PreparedStatement Statement = con.prepareStatement(sql);
  215.               //  Statement.setInt(1, id);
  216.                
  217.             //    int rowDeleted=Statement.executeUpdate();
  218.                
  219.               //  if(rowDeleted>0){
  220.                 //    System.out.println("A USER wa DELETED successfully");
  221.               //  }
  222.                
  223.                
  224.            }
  225.            
  226.         } catch (Exception e) {
  227.             System.out.println(e.getMessage());
  228.         }
  229.     }  
  230. }
  231.  
  232.         %--
  233.     Document   : harsh.jsp
  234.     Created on : 17 Aug, 2023, 3:14:33 PM
  235.     Author     : srki-078
  236. --%>
  237.  
  238. <%@page import = "java.sql.*, java.util.*, java.io.*"%>
  239. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  240. <!DOCTYPE html>
  241. <html>
  242.     <head>
  243.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  244.         <title>Database connection JSP Page</title>
  245.     </head>
  246.     <body>
  247.         <%! Connection con = null;%>
  248.         <%out.print("connection is null!");%>
  249.         <%!DataInputStream dis = new DataInputStream(System.in);%>
  250.         <%
  251.             try {
  252.  
  253.                 int id;
  254.                 String pname;
  255.                 String user = "root";
  256.                 String pass = "";
  257.                 String localhost = "jdbc:mysql://localhost:3310/Employee_det";
  258.  
  259.                 Class.forName("com.mysql.jdbc.Driver");
  260.                 con = DriverManager.getConnection(localhost, user, pass);
  261.  
  262.                 if (con != null) {
  263.                     out.println("Connected successfully!");
  264.                 }
  265.                 String sq = "Select * from Emp_Info";
  266.                 Statement stmt;
  267.  
  268.                 stmt = con.createStatement();
  269.                 ResultSet rs = stmt.executeQuery(sq);
  270.                 while (rs.next()) {
  271.                     out.println(rs.getInt(1) + " " + rs.getString(2));
  272.                 }
  273.                 con.close();
  274.             } catch (Exception ex) {
  275.                 out.println("Error found");
  276.             }%>
  277.     </body>
  278. </html>
  279.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement