Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Name:-
- Subject:- IPWCT
- Enrollment-No:-
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package Srki_r_jdbc;
- import java.sql.*;
- import java.io.*;
- /**
- *
- * @author srki-078
- */
- public class srki_Jdbc {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- Connection con = null;
- System.out.println("connection is null");
- DataInputStream dis = new DataInputStream(System.in);
- try {
- int id;
- String emp_name;
- Float salary;
- String Designation;
- String user = "root";
- String pass = "";
- String localhost = "jdbc:mysql://localhost:3310/Employee_det";
- // Class.forName("com.mysql.jdbc.Driver");
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection(localhost, user, pass);
- if(con!=null){
- System.out.println("Connected successfully");
- //
- // System.out.println("New User Inserted");
- // }
- //
- //
- //
- // System.out.println("Enter emp_name");
- // emp_name = dis.readLine();
- //
- // System.out.println("Enter salary");
- // salary = Float.parseFloat(dis.readLine());
- //
- // System.out.println("enter Designation");
- // Designation = dis.readLine();
- //
- // String sql ="INSERT INTO Emp_Info(pname,salary,designation) VALUES(?,?,?)";
- // PreparedStatement Statement=con.prepareStatement((sql));
- // Statement.setString(1, emp_name);
- // Statement.setFloat(2,salary);
- // Statement.setString(3,Designation);
- //
- // int rowsInserted = Statement.executeUpdate();
- // if(rowsInserted>0){
- // Statement stmt = con.createStatement();
- // Statement stmt = con.createStatement();
- // String sql ="select * from Emp_Info";
- // ResultSet rs = stmt.executeQuery(sql);
- //
- //
- // while (rs.next()){
- // System.out.println(rs.getInt("emp_id") + " " + rs.getString("emp_name"));
- // }
- // con.close();
- //
- // }
- // else{
- // System.out.println("can't connected");
- // UPDATE CODE
- System.out.println("enter ID");
- id=Integer.parseInt(dis.readLine());
- System.out.println("enter Name");
- emp_name=dis.readLine();
- String sql ="UPDATE Emp_Info SET emp_name=? WHERE emp_id=?";
- PreparedStatement statement = con.prepareStatement(sql);
- statement.setString(1,emp_name);
- statement.setInt(2, id);
- int rowUpdated=statement.executeUpdate();
- if(rowUpdated>0){
- System.out.println("An existing user was updated successfully");
- }
- // DELETE CODE
- // String sql = "DELETE from Emp_Info where emp_id=?";
- // System.out.println("enter emp_id");
- // id = Integer.parseInt(dis.readLine());
- // PreparedStatement Statement = con.prepareStatement(sql);
- // Statement.setInt(1, id);
- // int rowDeleted=Statement.executeUpdate();
- // if(rowDeleted>0){
- // System.out.println("A USER wa DELETED successfully");
- // }
- }
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- }
- }
- package harsh_r_jdbc;
- import java.sql.*;
- import java.io.*;
- /**
- *
- * @author srki-078
- */
- public class harsh_Jdbc {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- Connection con = null;
- System.out.println("connection is null");
- DataInputStream dis = new DataInputStream(System.in);
- try {
- int id;
- String emp_name;
- Float salary;
- String Designation;
- String user = "root";
- String pass = "";
- String localhost = "jdbc:mysql://localhost:3310/Employee_det";
- // Class.forName("com.mysql.jdbc.Driver");
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection(localhost, user, pass);
- if(con!=null){
- System.out.println("Connected successfully");
- //
- // System.out.println("New User Inserted");
- // }
- //
- //
- //
- // System.out.println("Enter emp_name");
- // emp_name = dis.readLine();
- //
- // System.out.println("Enter salary");
- // salary = Float.parseFloat(dis.readLine());
- //
- // System.out.println("enter Designation");
- // Designation = dis.readLine();
- //
- // String sql ="INSERT INTO Emp_Info(pname,salary,designation) VALUES(?,?,?)";
- // PreparedStatement Statement=con.prepareStatement((sql));
- // Statement.setString(1, emp_name);
- // Statement.setFloat(2,salary);
- // Statement.setString(3,Designation);
- //
- // int rowsInserted = Statement.executeUpdate();
- // if(rowsInserted>0){
- // Statement stmt = con.createStatement();
- // Statement stmt = con.createStatement();
- // String sql ="select * from Emp_Info";
- // ResultSet rs = stmt.executeQuery(sql);
- //
- //
- // while (rs.next()){
- // System.out.println(rs.getInt("emp_id") + " " + rs.getString("emp_name"));
- // }
- // con.close();
- //
- // }
- // else{
- // System.out.println("can't connected");
- // UPDATE CODE
- System.out.println("enter ID");
- id=Integer.parseInt(dis.readLine());
- System.out.println("enter Name");
- emp_name=dis.readLine();
- String sql ="UPDATE Emp_Info SET emp_name=? WHERE emp_id=?";
- PreparedStatement statement = con.prepareStatement(sql);
- statement.setString(1,emp_name);
- statement.setInt(2, id);
- int rowUpdated=statement.executeUpdate();
- if(rowUpdated>0){
- System.out.println("An existing user was updated successfully");
- }
- // DELETE CODE
- // String sql = "DELETE from Emp_Info where emp_id=?";
- // System.out.println("enter emp_id");
- // id = Integer.parseInt(dis.readLine());
- // PreparedStatement Statement = con.prepareStatement(sql);
- // Statement.setInt(1, id);
- // int rowDeleted=Statement.executeUpdate();
- // if(rowDeleted>0){
- // System.out.println("A USER wa DELETED successfully");
- // }
- }
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- }
- }
- %--
- Document : harsh.jsp
- Created on : 17 Aug, 2023, 3:14:33 PM
- Author : srki-078
- --%>
- <%@page import = "java.sql.*, java.util.*, java.io.*"%>
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Database connection JSP Page</title>
- </head>
- <body>
- <%! Connection con = null;%>
- <%out.print("connection is null!");%>
- <%!DataInputStream dis = new DataInputStream(System.in);%>
- <%
- try {
- int id;
- String pname;
- String user = "root";
- String pass = "";
- String localhost = "jdbc:mysql://localhost:3310/Employee_det";
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection(localhost, user, pass);
- if (con != null) {
- out.println("Connected successfully!");
- }
- String sq = "Select * from Emp_Info";
- Statement stmt;
- stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(sq);
- while (rs.next()) {
- out.println(rs.getInt(1) + " " + rs.getString(2));
- }
- con.close();
- } catch (Exception ex) {
- out.println("Error found");
- }%>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement