Guest User

Untitled

a guest
Mar 21st, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package jbdc1;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.util.Scanner;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. /**
  17. *
  18. * @author jnec
  19. */
  20. public class Jbdc1 {
  21. int id;
  22. String name;
  23. public static void show() throws SQLException
  24. {
  25. try {
  26. Class.forName("com.mysql.jdbc.Driver");
  27. String path;
  28. path = "jdbc:mysql://localhost:3306/jbdc1";
  29. Connection con=DriverManager.getConnection(path, "root", "mysql");
  30. java.sql.Statement stmt=con.createStatement();
  31. String sql="select * from teg";
  32. ResultSet rs=stmt.executeQuery(sql);
  33. while(rs.next())
  34. {
  35. int id=rs.getInt("id");
  36. String name=rs.getString("name");
  37. System.out.println("id="+id);
  38. System.out.println("name="+name);
  39. }
  40. con.close();
  41. stmt.close();
  42. rs.close();
  43. } catch (ClassNotFoundException | SQLException ex) {
  44. Logger.getLogger(Jbdc1.class.getName()).log(Level.SEVERE, null, ex);
  45. }
  46. }
  47. public static void insert(int id,String name)
  48. {
  49. try {
  50. Class.forName("com.mysql.jdbc.Driver");
  51. String path;
  52. path = "jdbc:mysql://localhost:3306/jbdc1";
  53. Connection con=DriverManager.getConnection(path, "root", "mysql");
  54. java.sql.Statement stmt=con.createStatement();
  55. String sql="insert into teg values("+id+",'"+name+"')";
  56. ResultSet rs=stmt.executeQuery(sql);
  57. try{
  58. int cnt=stmt.executeUpdate(sql);
  59. if(cnt>=0)
  60. {
  61. System.out.println("Record Inserted");
  62. }
  63. else
  64. {
  65. System.out.println("Record Not Inserted");
  66. }
  67. }
  68. catch(SQLException e)
  69. {
  70. System.out.println("Insertion Error="+e);
  71. }
  72. con.close();
  73. stmt.close();
  74. rs.close();
  75. } catch (ClassNotFoundException | SQLException ex) {
  76. Logger.getLogger(Jbdc1.class.getName()).log(Level.SEVERE, null, ex);
  77. }
  78. }
  79. public static void delete(int id)
  80. {
  81. try {
  82. Class.forName("com.mysql.jdbc.Driver");
  83. String path;
  84. path = "jdbc:mysql://localhost:3306/jbdc1" ;
  85. Connection con=DriverManager.getConnection(path, "root", "mysql");
  86. java.sql.Statement stmt=con.createStatement();
  87. String sql="delete from teg where id"
  88. + "="+id;
  89. ResultSet rs=stmt.executeQuery(sql);
  90. try{
  91. int cnt=stmt.executeUpdate(sql);
  92. if(cnt>=0)
  93. {
  94. System.out.println("Record dele");
  95. }
  96. else
  97. {
  98. System.out.println("Record Not del");
  99. }
  100. }
  101. catch(SQLException e)
  102. {
  103. System.out.println("Insertion Error="+e);
  104. }
  105. con.close();
  106. stmt.close();
  107. rs.close();
  108. } catch (SQLException | ClassNotFoundException ex) {
  109. Logger.getLogger(Jbdc1.class.getName()).log(Level.SEVERE, null, ex);
  110. }
  111. }
  112. /**
  113. * @param args the command line arguments
  114. */
  115.  
  116. /**
  117. *
  118. * @param args the command line arguments
  119. * @throws java.sql.SQLException
  120. */
  121. public static void main(String[] args) throws SQLException {
  122. // TODO code application logic here
  123. int ch;
  124. int id = 0;
  125. String name = null;
  126. System.out.println("1.Insert\t2.Delete\t3.Update\t4.Show");
  127. System.out.println("choice ");
  128. Scanner sc4=new Scanner(System.in);
  129. ch=sc4.nextInt();
  130.  
  131. do
  132. {
  133. switch(ch)
  134. {
  135.  
  136. case 1:
  137. System.out.println("enter id");
  138. Scanner sc=new Scanner(System.in);
  139. id=sc.nextInt();
  140. name=sc.next();
  141. insert(id, name);
  142. break;
  143. case 2:delete(id);
  144. System.out.println("enter id");
  145. Scanner sc1=new Scanner(System.in);
  146. id=sc1.nextInt();
  147. break;
  148.  
  149. case 3:show();
  150. System.out.println("id="+id);
  151. System.out.println("name="+name);
  152. break;
  153. }
  154.  
  155. }while(ch>=1);
  156. }
  157.  
  158. }
Add Comment
Please, Sign In to add comment