Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 jbdc1;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author jnec
- */
- public class Jbdc1 {
- int id;
- String name;
- public static void show() throws SQLException
- {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String path;
- path = "jdbc:mysql://localhost:3306/jbdc1";
- Connection con=DriverManager.getConnection(path, "root", "mysql");
- java.sql.Statement stmt=con.createStatement();
- String sql="select * from teg";
- ResultSet rs=stmt.executeQuery(sql);
- while(rs.next())
- {
- int id=rs.getInt("id");
- String name=rs.getString("name");
- System.out.println("id="+id);
- System.out.println("name="+name);
- }
- con.close();
- stmt.close();
- rs.close();
- } catch (ClassNotFoundException | SQLException ex) {
- Logger.getLogger(Jbdc1.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static void insert(int id,String name)
- {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String path;
- path = "jdbc:mysql://localhost:3306/jbdc1";
- Connection con=DriverManager.getConnection(path, "root", "mysql");
- java.sql.Statement stmt=con.createStatement();
- String sql="insert into teg values("+id+",'"+name+"')";
- ResultSet rs=stmt.executeQuery(sql);
- try{
- int cnt=stmt.executeUpdate(sql);
- if(cnt>=0)
- {
- System.out.println("Record Inserted");
- }
- else
- {
- System.out.println("Record Not Inserted");
- }
- }
- catch(SQLException e)
- {
- System.out.println("Insertion Error="+e);
- }
- con.close();
- stmt.close();
- rs.close();
- } catch (ClassNotFoundException | SQLException ex) {
- Logger.getLogger(Jbdc1.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- public static void delete(int id)
- {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String path;
- path = "jdbc:mysql://localhost:3306/jbdc1" ;
- Connection con=DriverManager.getConnection(path, "root", "mysql");
- java.sql.Statement stmt=con.createStatement();
- String sql="delete from teg where id"
- + "="+id;
- ResultSet rs=stmt.executeQuery(sql);
- try{
- int cnt=stmt.executeUpdate(sql);
- if(cnt>=0)
- {
- System.out.println("Record dele");
- }
- else
- {
- System.out.println("Record Not del");
- }
- }
- catch(SQLException e)
- {
- System.out.println("Insertion Error="+e);
- }
- con.close();
- stmt.close();
- rs.close();
- } catch (SQLException | ClassNotFoundException ex) {
- Logger.getLogger(Jbdc1.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- /**
- * @param args the command line arguments
- */
- /**
- *
- * @param args the command line arguments
- * @throws java.sql.SQLException
- */
- public static void main(String[] args) throws SQLException {
- // TODO code application logic here
- int ch;
- int id = 0;
- String name = null;
- System.out.println("1.Insert\t2.Delete\t3.Update\t4.Show");
- System.out.println("choice ");
- Scanner sc4=new Scanner(System.in);
- ch=sc4.nextInt();
- do
- {
- switch(ch)
- {
- case 1:
- System.out.println("enter id");
- Scanner sc=new Scanner(System.in);
- id=sc.nextInt();
- name=sc.next();
- insert(id, name);
- break;
- case 2:delete(id);
- System.out.println("enter id");
- Scanner sc1=new Scanner(System.in);
- id=sc1.nextInt();
- break;
- case 3:show();
- System.out.println("id="+id);
- System.out.println("name="+name);
- break;
- }
- }while(ch>=1);
- }
- }
Add Comment
Please, Sign In to add comment