Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- JDBC
- //STEP 1. Import required packagesimport java.sql.*;
- public class FirstExample {
- // JDBC driver name and database URL
- static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- static final String DB_URL = "jdbc:mysql://localhost/EMP";
- // Database credentials
- static final String USER = "username";
- static final String PASS = "password";
- public static void main(String[] args) {
- Connection conn = null;
- Statement stmt = null;
- try{
- //STEP 2: Register JDBC driver
- Class.forName("com.mysql.jdbc.Driver");
- //STEP 3: Open a connection
- System.out.println("Connecting to database...");
- conn = DriverManager.getConnection(DB_URL,USER,PASS);
- //STEP 4: Execute a query
- System.out.println("Creating statement...");
- stmt = conn.createStatement();
- String sql;
- sql = "SELECT id, first, last, age FROM Employees";
- ResultSet rs = stmt.executeQuery(sql);
- //STEP 5: Extract data from result set
- while(rs.next()){
- //Retrieve by column name
- int id = rs.getInt("id");
- int age = rs.getInt("age");
- String first = rs.getString("first");
- String last = rs.getString("last");
- //Display values
- System.out.print("ID: " + id);
- System.out.print(", Age: " + age);
- System.out.print(", First: " + first);
- System.out.println(", Last: " + last);
- }
- //STEP 6: Clean-up environment
- rs.close();
- stmt.close();
- conn.close();
- }catch(SQLException se){
- //Handle errors for JDBC
- se.printStackTrace();
- }catch(Exception e){
- //Handle errors for Class.forName
- e.printStackTrace();
- }finally{
- //finally block used to close resources
- try{
- if(stmt!=null)
- stmt.close();
- }catch(SQLException se2){
- }// nothing we can do
- try{
- if(conn!=null)
- conn.close();
- }catch(SQLException se){
- se.printStackTrace();
- }//end finally try
- }//end try
- System.out.println("Goodbye!");}//end main}//end FirstExample
- THREAD
- //16BCI0117 GOKULA KRISHNAN S
- class Poll {
- static private int a , b , c;
- //Synchronized method if vote for A
- static synchronized public void voteA () {
- a++;
- }
- //Synchronized method if vote for B
- static synchronized public void voteB () {
- b++;
- }
- //Synchronized method if vote for C
- static synchronized public void voteC () {
- c++;
- }
- //Getter Methods
- static public int getA () {
- return a;
- }
- static public int getB () {
- return b;
- }
- static public int getC () {
- return c;
- }
- }
- class VoteCount {
- //Switch case statements to cast votes
- private static void voteCandidate ( char ch ) {
- switch (ch) {
- case 'A' :
- Poll.voteA();
- break ;
- case 'B' :
- Poll.voteB();
- break ;
- case 'C' :
- Poll.voteC();
- break ;
- }
- }
- public static void main ( String [] args ) throws InterruptedException {
- int strength = 240 ;
- char votes[] = new char[strength];
- char candidates[] = { 'A' , 'B' , 'C' };
- // randomly vote candidates
- for ( int i = 0; i < strength; i++) {
- int ch = (int) (Math.random() * 3 );
- char cand = candidates[ch];
- votes[i] = cand;
- }
- int r1 = 0 , r2 = strength / 3 , r3 = strength / 3 * 2 ;
- // create threads with anonymous class declaration
- Thread t1 = new Thread() {
- public void run () {
- for ( int i = r1; i < r2; i++)
- voteCandidate(votes[i]);
- }
- };
- Thread t2 = new Thread() {
- public void run () {
- for ( int i = r2; i < r3; i++)
- voteCandidate(votes[i]);
- }
- };
- Thread t3 = new Thread() {
- public void run () {
- for ( int i = r3; i < strength; i++)
- voteCandidate(votes[i]);
- }
- };
- t1.start();
- t2.start();
- t3.start();
- // join main thread
- t1.join();
- t2.join();
- t3.join();
- int a , b , c;
- a = Poll.getA();
- b = Poll.getB();
- c = Poll.getC();
- System.out.println( "voting results: " );
- System.out.println( "A: " + a);
- System.out.println( "B: " + b);
- System.out.println( "C: " + c);
- System.out.print( "Congrats " );
- // get max vote candidate
- if (a > b)
- if (c > a)
- System.out.print( "C " );
- else
- System.out.print( "A " );
- else if (c > b)
- System.out.print( "C " );
- else
- System.out.print( "B " );
- }
- }
- FILE
- import java.io.FileOutputStream;
- import java.io.ObjectOutputStream;
- import java.io.FileInputStream;
- import java.io.ObjectInputStream;
- import java.io.*;
- import java.util.*;
- class File {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- try {
- FileOutputStream fout = new FileOutputStream("student.txt");
- ObjectOutputStream obout = new ObjectOutputStream(fout);
- for (int i = 0; i < 2; i++) {
- String name = scan.nextLine();
- String id = scan.nextLine();
- Student student = new Student(name, id);
- obout.writeObject(student);
- }
- obout.close();
- fout.close();
- } catch (Exception e) {
- System.out.println(e);
- }
- try {
- FileInputStream fin = new FileInputStream("student.txt");
- ObjectInputStream oin = new ObjectInputStream(fin);
- Student obj;
- while (oin.available() != -1) {
- obj = (Student) oin.readObject();
- System.out.println(obj);
- }
- oin.close();
- fin.close();
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- }
- class Student implements Serializable {
- String name, id;
- public Student() {
- }
- public Student(String name, String id) {
- this.name = name;
- this.id = id;
- }
- public String getName() {
- return this.name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getId() {
- return this.id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public Student name(String name) {
- this.name = name;
- return this;
- }
- public Student id(String id) {
- this.id = id;
- return this;
- }
- public String toString() {
- return "{" + " name='" + getName() + "'" + ", id='" + getId() + "'" + "}";
- }
- }
Add Comment
Please, Sign In to add comment