Guest User

Untitled

a guest
Feb 21st, 2018
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.29 KB | None | 0 0
  1. TO DISPLAY THE CONTENTS OF THE TABLE:
  2. import java.sql.*;
  3. public class Database {
  4.     static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
  5.     static final String DB_URL="jdbc:mysql://localhost/school";
  6.     static final String USER="root";
  7.     public static void main(String[]args) {
  8.         Connection conn=null;
  9.          Statement stmt=null;
  10.          try {
  11.              Class.forName("com.mysql.jdbc.Driver");
  12.              System.out.println("Connecting to database...");
  13.              conn=DriverManager.getConnection(DB_URL,USER,"");
  14.              System.out.println("Creating statement...");
  15.              stmt=conn.createStatement();
  16.              String sql;
  17.              sql="SELECT * FROM Teachers";
  18.              ResultSet rs=stmt.executeQuery(sql);
  19.              while(rs.next()) {
  20.                  int id=rs.getInt("id_no");
  21.                  String name=rs.getString("name");
  22.                  String first=rs.getString("address");
  23.                  System.out.print("ID:"+id);
  24.                  System.out.print(",Name"+name);
  25.                  System.out.print(",address:"+first);
  26.              }
  27.              rs.close();
  28.              stmt.close();
  29.              conn.close();
  30.          }catch(SQLException se) {
  31.             se.printStackTrace();
  32.          }catch(Exception e) {
  33.              e.printStackTrace();
  34.          }finally {
  35.              try {
  36.                  if(stmt!=null)
  37.                      stmt.close();
  38.              }catch(SQLException se2) {
  39.                  
  40.              }
  41.          try {
  42.              if(conn!=null)
  43.                  conn.close();
  44.          }catch(SQLException se) {
  45.              se.printStackTrace();
  46.          }
  47.          System.out.println("Goodbye!");
  48.          }
  49.     }
  50. }
  51.    
  52.    
  53.  
  54. OUTPUT:
  55. Connecting to database...
  56. Creating statement...
  57. ID:1,Namepooja,address:xyz ID:10,Namexxx,address:YXZID:19,Nameyyy,address:YzDGoodbye!
  58. ======================================================================================================
  59.        
  60. import java.sql.*;
  61. import com.mysql.jdbc.Statement;
  62. public class Names {
  63.     static final String JDBC_DRIVER="comm.mysql.jdbc.Driver";
  64.     static final String DB_URL="jdbc:mysql://localhost/school";
  65.     static final String USER="root";
  66.     static final String PASS="";
  67.     public static void main(String[]args) {
  68.         Connection conn=null;
  69.         Statement stmt=null;
  70.         try {
  71.             Class.forName("com.mysql.jdbc.Driver");
  72.             System.out.println("Connecting to database...");
  73.             conn=DriverManager.getConnection(DB_URL,USER,PASS);
  74.             System.out.println("Creating Statement...");
  75.             stmt=(Statement) conn.createStatement();
  76.             String sql;
  77.             sql="SELECT * FROM Teachers";
  78.             ResultSet rs=stmt.executeQuery(sql);
  79.             System.out.print("Name\n");
  80.             while(rs.next()) {
  81.                 String name=rs.getString("name");
  82.                
  83.                 System.out.print(name+"\n");
  84.             }
  85.             rs.close();
  86.             stmt.close();
  87.             conn.close();  
  88.         }catch(Exception se){
  89.             se.printStackTrace();
  90.         }finally {
  91.             try {
  92.                 if(stmt!=null)
  93.                     stmt.close();
  94.             }catch(SQLException se2) {
  95.         }
  96.             try {
  97.                 if(conn!=null)
  98.                     conn.close();
  99.             }catch(SQLException se){
  100.                 se.printStackTrace();
  101.             }
  102.     }
  103.      System.out.println("\nGoodBye!");
  104. }
  105. }
  106.  
  107. OUTPUT:
  108. Connecting to database...
  109. Creating Statement...
  110. Name
  111. pooja
  112. xxx
  113. yyy
  114.  
  115. GoodBye!
  116. =====================================================================================================
  117. import java.sql.*;
  118. public class Table {
  119.     static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
  120.     static final String DB_URL="jdbc:mysql://localhost/emp";
  121.     static final String USER="root";
  122.     static final String PASS="";
  123.     public static void main(String[]args) throws ClassNotFoundException {
  124.         Connection conn=null;
  125.         Statement stmt=null;
  126.         try {
  127.             Class.forName("com.mysql.jdbc.Driver");
  128.             System.out.println("Connecting to a selected database...");
  129.             conn=DriverManager.getConnection(DB_URL,USER,PASS);
  130.             System.out.println("Connected database successfully...");
  131.             System.out.println("Creating table in given database...");
  132.             stmt=conn.createStatement();
  133.             String sql="Create TABLE emp1"+
  134.             "(id INTEGER not NULL,"+
  135.                     "first VARCHAR(20),"+
  136.                     "last VARCHAR(10),"+
  137.                     "age INTEGER,"+
  138.                     "PRIMARY KEY(id))";
  139.             stmt.executeUpdate(sql);
  140.             System.out.println("Created table in given database...");
  141.         }catch(SQLException se) {
  142.             se.printStackTrace();
  143.         }finally {
  144.             try {
  145.                 if(stmt!=null)
  146.                     conn.close();
  147.             }catch(SQLException se) {
  148.         }
  149.             try {
  150.                 if(conn!=null)
  151.                     conn.close();
  152.             }catch(SQLException se) {
  153.                 se.printStackTrace();
  154.             }
  155.     }
  156.       System.out.println("Goodbye!");
  157. }
  158. }
  159. ================================================================================
  160. import java.sql.DriverManager;
  161. import java.sql.ResultSet;
  162. import java.sql.SQLException;
  163.  
  164. import com.mysql.jdbc.Connection;
  165. import com.mysql.jdbc.PreparedStatement;
  166.  
  167. public class Preparedstmt {
  168.     static final String JDBC_DRIVER="com..mysql.jdbc.Driver";  
  169.     static final String DB_URL="jdbc:mysql://localhost/school";
  170.     static final String USER="root";
  171.     static final String PASS="";
  172.     public static void main(String[]args) {
  173.         Connection conn=null;
  174.         PreparedStatement stmt=null;
  175.         try {
  176.             Class.forName("com.mysql.jdbc.Driver");
  177.  
  178.              
  179.               System.out.println("Connecting to database...");
  180.               conn = (Connection) DriverManager.getConnection(DB_URL,USER,PASS);
  181.              
  182.               System.out.println("Creating statement...");
  183.               String sql = "UPDATE teachers set name=? WHERE id_no=?";
  184.               stmt = (PreparedStatement) conn.prepareStatement(sql);
  185.            
  186.               stmt.setString(1, "pillai");  
  187.               stmt.setInt(2, 19);
  188.               int rows = stmt.executeUpdate();
  189.               System.out.println("Rows impacted : " + rows );
  190.              
  191.               sql = "SELECT * FROM teachers";
  192.               ResultSet rs = stmt.executeQuery(sql);
  193.               while(rs.next()){
  194.                      
  195.                      int id  = rs.getInt("id_no");
  196.                      String name = rs.getString("name");
  197.                    
  198.                      
  199.  
  200.                      
  201.                      System.out.print("ID:" +id);
  202.                      System.out.print(", Name " + name);
  203.                      
  204.      }
  205.                  
  206.                   rs.close();
  207.                   stmt.close();
  208.                   conn.close();
  209.                }catch(SQLException se){
  210.                  
  211.                   se.printStackTrace();
  212.                }catch(Exception e){
  213.                  
  214.                   e.printStackTrace();
  215.                }finally{
  216.                  
  217.                   try{
  218.                      if(stmt!=null)
  219.                         stmt.close();
  220.                   }catch(SQLException se2){
  221.                   }
  222.                   try{
  223.                      if(conn!=null)
  224.                         conn.close();
  225.                   }catch(SQLException se){
  226.                      se.printStackTrace();
  227.                   }
  228.                }
  229.                System.out.println("\n Goodbye!");
  230.        
  231.  
  232.  
  233.     }
  234.  
  235.  
  236.  
  237.         }
Add Comment
Please, Sign In to add comment