Tuesday, October 16, 2012

my sql connectivity

Connection  to demonstrate My sql connectivity to student with name and id filled 

 import java.sql.*;  
 import java.util.logging.Level;  
 import java.util.logging.Logger;  
 public class mysql1 {  
      public static void main(String[] args)   
      {  
     try {  
       System.out.println("My sql Connect Example.");  
       Connection conn = null;  
         Class.forName("com.mysql.jdbc.Driver").newInstance();  
         conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","root","");  
         System.out.println("Connected to the database");  
         Statement statement = conn.createStatement();   
         //create table statement  
         String createStudentTable = "CREATE TABLE student(NAME VARCHAR(64),ID number(10))";                                      
         statement.executeUpdate(createStudentTable);   
         statement.executeUpdate("INSERT INTO student VALUES ( 'UML User Guide','1909')");  
         statement.executeUpdate("INSERT INTO student VALUES ( 'UML User Guide2','2909')");  
         statement.execute("UPDATE student SET id=27889 WHERE id=28889");  
 statement.executeQuery("delete * from student");  
  conn.close();  
         System.out.println("Disconnected from database");  
     } catch (InstantiationException ex) {  
       Logger.getLogger(mysql1.class.getName()).log(Level.SEVERE, null, ex);  
     } catch (IllegalAccessException ex) {  
       Logger.getLogger(mysql1.class.getName()).log(Level.SEVERE, null, ex);  
     } catch (SQLException ex) {  
       Logger.getLogger(mysql1.class.getName()).log(Level.SEVERE, null, ex);  
     } catch (ClassNotFoundException ex) {  
       Logger.getLogger(mysql1.class.getName()).log(Level.SEVERE, null, ex);  
     }   
      }    
 }  

No comments:

Post a Comment