0% found this document useful (0 votes)
23 views

Java - Io. Java - Sql. : Void Int

This Java program uses JDBC to connect to a database and perform CRUD operations on an employee table. It imports necessary Java and SQL libraries, establishes a connection to the database, defines methods to display options and format output. Based on user input, it executes SQL statements to select, insert, update or delete records, handling exceptions if errors occur.

Uploaded by

amol
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Java - Io. Java - Sql. : Void Int

This Java program uses JDBC to connect to a database and perform CRUD operations on an employee table. It imports necessary Java and SQL libraries, establishes a connection to the database, defines methods to display options and format output. Based on user input, it executes SQL statements to select, insert, update or delete records, handling exceptions if errors occur.

Uploaded by

amol
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. import java.io.

*;

2. import java.sql.*;

3. public class jdbcprg {

4.

5. static void myLine(){

6. for (int i=1;i<=80;i++)

7. {

8. System.out.print("*");

9. }

10. System.out.println();

11. }

12. public static void main(String[] args) {

13. Connection con;

14. Statement st;

15. BufferedReader bin;

16. ResultSet rs;

17. ResultSetMetaData rm;

18. String eno, ename, salary;

19. int ch, nof;

20.

21. try

22. {

23. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

24. con = DriverManager.getConnection("jdbc:odbc:karthik");

25. st = con.createStatement();
26. bin = new BufferedReader(new
InputStreamReader(System.in));

27. while(true){

28. System.out.println("Choose Option");

29. System.out.println("1. Select");

30. System.out.println("2. Insert");

31. System.out.println("3. Update");

32. System.out.println("4. Delete");

33. System.out.println("0. Exit");

34. ch = Integer.parseInt(bin.readLine());

35. if (ch==1)

36.

37. {

38. rs = st.executeQuery("select * from emp");

39. rm = rs.getMetaData();

40. nof = rm.getColumnCount();

41. myLine();

42. for(int i=1; i<=nof; i++)

43. {

44. System.out.print(rm.getColumnName(i)+"\t\t");

45. }

46. System.out.println();

47. myLine();

48. while(rs.next())

49. {

50. for(int i=1; i<=nof; i++)


51. {

52. System.out.print(rs.getString(i) +"\t\t");

53. }

54. System.out.println();

55. }

56. myLine();

57.

58. }

59. else if(ch==2)

60. {

61. do

62. {

63. System.out.println("Enter E.No");

64. eno = bin.readLine();

65. System.out.println("Enter Name");

66. ename = bin.readLine();

67. System.out.println("Enter Salary");

68. salary = bin.readLine();

69. st.execute("insert into emp


values("+eno+",'"+ename+"',"+salary+")");

70. System.out.println("1 Record inserted");

71. System.out.println("Continue ? [y/n]");

72. eno = bin.readLine();

73. }while(eno.equalsIgnoreCase("y"));

74. }

75. else if(ch==3)


76. {

77. System.out.println("Enter E.No. to Edit :");

78. eno = bin.readLine();

79. System.out.println("Enter Name");

80. ename = bin.readLine();

81. System.out.println("Enter Salary");

82. salary = bin.readLine();

83. st.execute("update emp set ename='"+ename+"',


salary="+salary+" where eno ="+eno);

84. System.out.println("1 Record Updated");

85.

86. }

87. else if(ch==4)

88. {

89. System.out.println("Enter E.No. to Delete :");

90. eno = bin.readLine();

91. st.execute("delete from emp where eno ="+eno);

92. System.out.println("Record Deleted");

93. }

94. else if(ch==0)

95. {

96. bin.close();

97. con.close();

98. System.exit(0);

99. }

100. }
101. }

102. catch(Exception e)

103. {

104. System.out.println("Error :"+e.getMessage());

105. }

106. }

107. }

You might also like