Guest User

Ip practical

a guest
Jan 29th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.92 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. IP PROJECT
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34. Made by: Manjinder Singh
  35.                   12th –I
  36.                   31 roll no.
  37. Submitted to: Mr. Rajeev Kumar
  38.                         (IP teacher)
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. Acknowledgement
  56. This project would have never been a success without the help of my friends, family and our IP teacher Mr. Rajeev Kumar. Special thanks to the uploaded for providing the info on the project online.
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. Java Data Base Connectivity (JDBC)
  69. A JDBC driver is a software component enabling a Java application to interact with a database. To connect with individual databases, JDBC (the Java Database Connectivity API) requires drivers for each database. The JDBC driver gives out the connection to the database and implements the protocol for transferring the query and result between client and database.
  70. In this project we will make two tables’ “teacher” and “user”. We will make a program in net beans to connect to the table “user” to authorize the username and password and then we will make program to delete, update, select and insert in the table “teacher”.
  71.  
  72.  
  73.  
  74. SQL query for forming table “user”
  75. Cerate table users (‘username’ varchar(20) , ‘password’ varcahr(16) , primary key(‘username’) ) ;
  76. Insert into users values (‘admin’, ‘abc123’);
  77.  
  78. Username    Password
  79.  
  80. admin  
  81. abc123
  82.  
  83. SQL query for forming table “teacher”
  84. Create table teacher (‘TEACHER_ID’ integer(6) , ‘TEACHER_NAME’ varchar(20) , ‘TEACHER_SALARY’ integer(6) , primary key(TEACHER_ID) )
  85.  
  86. TEACHER_ID  TEACHER_NAME    TEACHER_SALARY
  87. -   -   -
  88.  
  89.  
  90.  
  91. i.  Login page
  92. > Login button
  93. String un, pass ;
  94. try
  95. {
  96.  
  97. un =jTextField1.getText ();
  98. pass =jPasswordField1.getText ();
  99. master   ob=new master();
  100. Class.forName ("com.mysql.jdbc.Driver");
  101. String connectionUrlTOSQL = "jdbc:mysql://localhost/school?user=root";
  102. Connection con = DriverManager.getConnection (connectionUrlTOSQL);
  103. Statement ss=con.createStatement ();
  104. ResultSet rs=ss.executeQuery ("select * from users where uname='" + un + "' and pwd='" + pass + "'");
  105.  
  106. If (rs.first ())
  107. {ob.setVisible (true);}
  108.  
  109. Else
  110.  
  111. {
  112. JOptionPane.showMessageDialog (this, “Wrong user name or pwd");
  113. }
  114.  
  115. }
  116.  
  117. catch(Exception e)
  118. {
  119. System.out.println("SQL Exception: "+ e.toString());
  120. }
  121.  
  122.  
  123.  
  124.  
  125. Login fail
  126.  
  127.  
  128. Login successful
  129.  
  130. ii. Main menu
  131.  
  132. > Insert button
  133.  
  134. insert o=new insert();
  135. o.setVisible(true);
  136.  
  137. > delete button
  138.  
  139. delete od=new delete();
  140. od.setVisible(true);
  141.  
  142. > update button
  143.  
  144. update ou=new update();
  145. ou.setVisible(true);
  146.  
  147. > select button
  148.  
  149. select os=new select();
  150. os.setVisible(true);
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169. Main page
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177. iii.    Insert
  178. > Insert button
  179.  
  180. String teachID, teachName, teachSal ,q;
  181. teachID   =jTextField1.getText();
  182. teachName =jTextField2.getText();
  183. teachSal  =jTextField3.getText();
  184.  
  185. try
  186. {
  187. Class.forName("com.mysql.jdbc.Driver");
  188. String connectionUrl = "jdbc:mysql://localhost/school?user=root";
  189. Connection con = DriverManager.getConnection(connectionUrl);
  190. Statement ss=con.createStatement();
  191. q="insert into teacher values(" + teachID + ",'" + teachName + "'," + teachSal + ")";
  192. ss.executeUpdate(q);
  193. JOptionPane.showMessageDialog(this, "Row Inserted");
  194. }
  195.  
  196. catch(SQLException e)
  197. {
  198. System.out.println("SQL Exception: "+ e.toString());
  199. }
  200.  
  201. catch(Exception e)
  202. {
  203. System.out.println("SQL Exception: "+ e.toString());
  204. }
  205.  
  206.  
  207.  
  208.  
  209.  
  210. Insertion
  211.  
  212.  
  213.  
  214. iv. Delete
  215. > Delete button
  216.  string teachID,q;
  217. teachID=jTextField1.getText();
  218.  
  219. try {
  220. Class.forName("com.mysql.jdbc.Driver");
  221. String connectionUrl = "jdbc:mysql://localhost/school?user=root";
  222. Connection con = DriverManager.getConnection(connectionUrl);
  223. Statement ss=con.createStatement();
  224. q="delete from teacher where TEACHER_ID =" + teachID ;
  225. ss.executeUpdate(q);
  226. JOptionPane.showMessageDialog(this, "Row Deleted");
  227. }
  228.  
  229.  catch(SQLException e)
  230. {
  231. System.out.println("SQL Exception: "+ e.toString());
  232.  
  233. } catch(Exception e)
  234. {
  235. System.out.println("SQL Exception: "+ e.toString());
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243. Delete from the table
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250. v.  Update
  251. > update button
  252.  String teachID , teachName , teachsal ;
  253.   teachID    =jTextField1.getText();
  254.   teachName  =jTextField2.getText();
  255.   teachsal   =jTextField3.getText();
  256.  
  257.   try {
  258.   Class.forName("com.mysql.jdbc.Driver");
  259.   String connectionUrl = "jdbc:mysql://localhost/school?user=root";
  260.   Connection con = DriverManager.getConnection(connectionUrl);
  261.   Statement ss=con.createStatement();
  262.   q ="update teacher set TEACHER_NAME ='" + teachName + "',sal=" + teachsal + " where
  263.  TEACHER_ID=" + teachID;
  264.   ss.executeUpdate(q);
  265.   JOptionPane.showMessageDialog(this, "Row Updated");
  266.   }
  267.   catch(SQLException e)
  268.   { System.out.println("SQL Exception: "+ e.toString()); }
  269.   catch(Exception e)
  270.  { System.out.println("SQL Exception: "+ e.toString()); }
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281. Update
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289. vi. Select
  290. > select button
  291. String teachID;
  292. teachID =jTextField1.getText();
  293. DefaultTableModel model =(DefaultTableModel) jTable1.getModel();
  294.  
  295. if (rows>0)
  296. {
  297. for (int i=0; i<rows; i++)
  298. { model.removeRow(i); }
  299. }
  300. try
  301. {
  302. Class.forName("com.mysql.jdbc.Driver");
  303. Connectioncon=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/school?user=root");
  304. Statement stmt = (Statement) con.createStatement();
  305. String query;
  306. if(teachID.compareTo("")==0)
  307. query="SELECT * FROM teacher";
  308. else
  309. query="SELECT * FROM teacher where TEACH_ID=" + teachID;
  310. ResultSet rs = stmt.executeQuery(query);
  311.  
  312. while(rs.next())
  313. {
  314. String ID = rs.getString ("teachID");
  315. String eName = rs.getString ("teachName");
  316. String sal = rs.getString ("teachSal");
  317. model.addRow (new Object[]{teachID, teachName, teachSal });
  318. }
  319. }
  320. catch(Exception e)
  321. { System.out.println("SQL Exception: "+ e.toString()); }
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335. Select
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346. Bibliography:
  347.  
  348. www.google.com
  349.  
  350. www.wikipedia.com
  351.  
  352. www.ncert.com
  353.  
  354. www.hackingstars.com
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361. Thank YOU
Add Comment
Please, Sign In to add comment