Vikhyath_11

7

Jul 28th, 2024
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. package javajdbc;
  2. import java.sql.*;
  3. import javax.sql.*;
  4. import java.util.*;
  5.  
  6. public class Javajdbc {
  7.  
  8. public static void main(String[] args) {
  9. Scanner sc = new Scanner(System.in);
  10. String usn,name,dept;
  11. System.out.println("Enter Name:");
  12. name= sc.next();
  13. System.out.println("Enter USN:");
  14. usn= sc.next();
  15. System.out.println("Enter Dept.:");
  16. dept= sc.next();
  17. try{
  18. Class.forName("com.mysql.cj.jdbc.Driver");
  19. Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/Student","root","root");
  20. Statement stmt = con.createStatement();
  21. String q1 = "insert into student values('" +usn+"','"+name+"','"+dept+"')";
  22.  
  23. stmt.executeUpdate(q1);
  24. }
  25. catch(Exception e){
  26. System.out.println(e);
  27. }
  28. }
  29.  
  30. }
  31.  
  32.  
  33.  
  34.  
  35.  
  36. package javajdbc;
  37.  
  38. import java.sql.*;
  39. import javax.sql.*;
  40. public class output {
  41. public static void main(String[] args) {
  42.  
  43.  
  44. try{
  45. Class.forName("com.mysql.jdbc.Driver");
  46. Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root");
  47. Statement stmt = con.createStatement();
  48. String q2 = "select * from student";
  49. ResultSet rs = stmt.executeQuery(q2);
  50. while(rs.next()){
  51. System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
  52. }
  53. }
  54. catch(Exception e){
  55. System.out.println(e);
  56. }
  57. }
  58. }
  59.  
  60.  
  61.  
Advertisement
Add Comment
Please, Sign In to add comment