Guest User

Untitled

a guest
Apr 1st, 2019
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. JDBC
  2. //STEP 1. Import required packagesimport java.sql.*;
  3. public class FirstExample {
  4. // JDBC driver name and database URL
  5. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  6. static final String DB_URL = "jdbc:mysql://localhost/EMP";
  7.  
  8. // Database credentials
  9. static final String USER = "username";
  10. static final String PASS = "password";
  11.  
  12. public static void main(String[] args) {
  13. Connection conn = null;
  14. Statement stmt = null;
  15. try{
  16. //STEP 2: Register JDBC driver
  17. Class.forName("com.mysql.jdbc.Driver");
  18.  
  19. //STEP 3: Open a connection
  20. System.out.println("Connecting to database...");
  21. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  22.  
  23. //STEP 4: Execute a query
  24. System.out.println("Creating statement...");
  25. stmt = conn.createStatement();
  26. String sql;
  27. sql = "SELECT id, first, last, age FROM Employees";
  28. ResultSet rs = stmt.executeQuery(sql);
  29.  
  30. //STEP 5: Extract data from result set
  31. while(rs.next()){
  32. //Retrieve by column name
  33. int id = rs.getInt("id");
  34. int age = rs.getInt("age");
  35. String first = rs.getString("first");
  36. String last = rs.getString("last");
  37.  
  38. //Display values
  39. System.out.print("ID: " + id);
  40. System.out.print(", Age: " + age);
  41. System.out.print(", First: " + first);
  42. System.out.println(", Last: " + last);
  43. }
  44. //STEP 6: Clean-up environment
  45. rs.close();
  46. stmt.close();
  47. conn.close();
  48. }catch(SQLException se){
  49. //Handle errors for JDBC
  50. se.printStackTrace();
  51. }catch(Exception e){
  52. //Handle errors for Class.forName
  53. e.printStackTrace();
  54. }finally{
  55. //finally block used to close resources
  56. try{
  57. if(stmt!=null)
  58. stmt.close();
  59. }catch(SQLException se2){
  60. }// nothing we can do
  61. try{
  62. if(conn!=null)
  63. conn.close();
  64. }catch(SQLException se){
  65. se.printStackTrace();
  66. }//end finally try
  67. }//end try
  68. System.out.println("Goodbye!");}//end main}//end FirstExample
  69.  
  70.  
  71. THREAD
  72. //16BCI0117 GOKULA KRISHNAN S
  73. class Poll {
  74. static private int a , b , c;
  75. //Synchronized method if vote for A
  76. static synchronized public void voteA () {
  77. a++;
  78. }
  79. //Synchronized method if vote for B
  80. static synchronized public void voteB () {
  81. b++;
  82. }
  83. //Synchronized method if vote for C
  84. static synchronized public void voteC () {
  85. c++;
  86. }
  87. //Getter Methods
  88. static public int getA () {
  89. return a;
  90. }
  91. static public int getB () {
  92. return b;
  93. }
  94. static public int getC () {
  95. return c;
  96. }
  97. }
  98. class VoteCount {
  99. //Switch case statements to cast votes
  100. private static void voteCandidate ( char ch ) {
  101. switch (ch) {
  102. case 'A' :
  103. Poll.voteA();
  104. break ;
  105. case 'B' :
  106. Poll.voteB();
  107. break ;
  108. case 'C' :
  109. Poll.voteC();
  110. break ;
  111. }
  112. }
  113. public static void main ( String [] args ) throws InterruptedException {
  114. int strength = 240 ;
  115. char votes[] = new char[strength];
  116. char candidates[] = { 'A' , 'B' , 'C' };
  117. // randomly vote candidates
  118. for ( int i = 0; i < strength; i++) {
  119. int ch = (int) (Math.random() * 3 );
  120. char cand = candidates[ch];
  121. votes[i] = cand;
  122. }
  123. int r1 = 0 , r2 = strength / 3 , r3 = strength / 3 * 2 ;
  124. // create threads with anonymous class declaration
  125. Thread t1 = new Thread() {
  126. public void run () {
  127. for ( int i = r1; i < r2; i++)
  128. voteCandidate(votes[i]);
  129. }
  130. };
  131. Thread t2 = new Thread() {
  132. public void run () {
  133. for ( int i = r2; i < r3; i++)
  134. voteCandidate(votes[i]);
  135. }
  136. };
  137. Thread t3 = new Thread() {
  138. public void run () {
  139. for ( int i = r3; i < strength; i++)
  140. voteCandidate(votes[i]);
  141. }
  142. };
  143. t1.start();
  144. t2.start();
  145. t3.start();
  146. // join main thread
  147. t1.join();
  148. t2.join();
  149. t3.join();
  150. int a , b , c;
  151. a = Poll.getA();
  152. b = Poll.getB();
  153. c = Poll.getC();
  154. System.out.println( "voting results: " );
  155. System.out.println( "A: " + a);
  156. System.out.println( "B: " + b);
  157. System.out.println( "C: " + c);
  158. System.out.print( "Congrats " );
  159. // get max vote candidate
  160. if (a > b)
  161. if (c > a)
  162. System.out.print( "C " );
  163. else
  164. System.out.print( "A " );
  165. else if (c > b)
  166. System.out.print( "C " );
  167. else
  168. System.out.print( "B " );
  169. }
  170. }
  171.  
  172. FILE
  173. import java.io.FileOutputStream;
  174. import java.io.ObjectOutputStream;
  175. import java.io.FileInputStream;
  176. import java.io.ObjectInputStream;
  177. import java.io.*;
  178. import java.util.*;
  179.  
  180. class File {
  181. public static void main(String[] args) {
  182. Scanner scan = new Scanner(System.in);
  183. try {
  184. FileOutputStream fout = new FileOutputStream("student.txt");
  185. ObjectOutputStream obout = new ObjectOutputStream(fout);
  186. for (int i = 0; i < 2; i++) {
  187. String name = scan.nextLine();
  188. String id = scan.nextLine();
  189. Student student = new Student(name, id);
  190. obout.writeObject(student);
  191.  
  192. }
  193. obout.close();
  194. fout.close();
  195. } catch (Exception e) {
  196. System.out.println(e);
  197. }
  198.  
  199. try {
  200. FileInputStream fin = new FileInputStream("student.txt");
  201. ObjectInputStream oin = new ObjectInputStream(fin);
  202. Student obj;
  203. while (oin.available() != -1) {
  204. obj = (Student) oin.readObject();
  205. System.out.println(obj);
  206. }
  207. oin.close();
  208. fin.close();
  209. } catch (Exception e) {
  210. System.out.println(e);
  211. }
  212.  
  213. }
  214. }
  215.  
  216. class Student implements Serializable {
  217. String name, id;
  218.  
  219. public Student() {
  220. }
  221.  
  222. public Student(String name, String id) {
  223. this.name = name;
  224. this.id = id;
  225. }
  226.  
  227. public String getName() {
  228. return this.name;
  229. }
  230.  
  231. public void setName(String name) {
  232. this.name = name;
  233. }
  234.  
  235. public String getId() {
  236. return this.id;
  237. }
  238.  
  239. public void setId(String id) {
  240. this.id = id;
  241. }
  242.  
  243. public Student name(String name) {
  244. this.name = name;
  245. return this;
  246. }
  247.  
  248. public Student id(String id) {
  249. this.id = id;
  250. return this;
  251. }
  252.  
  253. public String toString() {
  254. return "{" + " name='" + getName() + "'" + ", id='" + getId() + "'" + "}";
  255. }
  256.  
  257. }
Add Comment
Please, Sign In to add comment