blooming8

MyJDBC

May 6th, 2022
1,369
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 1 0
  1. import java.sql.DriverManager;
  2. import java.sql.Connection;
  3. import java.sql.Statement;
  4. import java.sql.PreparedStatement;
  5. import java.sql.CallableStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. public class MyJDBC
  10. {
  11.     private static String url = "jdbc:mysql://localhost:3306/mydb";
  12.     private static String user = "root";
  13.     private static String psw = "0_mysql_1";
  14.     private static Connection myConnection = null;
  15.     private static Statement myStatement = null;
  16.     private static PreparedStatement myPreparedStatement = null;
  17.     private static CallableStatement myCallableStatement = null;
  18.     private static ResultSet myResultSet = null;
  19.    
  20.     private static String nome;
  21.     private static String cognome;
  22.     private static int eta;
  23.    
  24.     public static void main(String[] args)
  25.     {
  26.         try
  27.         {
  28.             myConnection = DriverManager.getConnection(url, user, psw);
  29.             System.out.println("Connessione al db riuscita.\n");
  30.            
  31.             myStoredUpdate();
  32.            
  33.            
  34.             mySelect();
  35.            
  36.         }
  37.         catch (SQLException e)
  38.         {
  39.             System.out.printf("Errore: %s", e.getMessage());   
  40.         }
  41.     }
  42.    
  43.     // CRUD - Create, Read, Update, Delete
  44.    
  45.     // SELECT
  46.     public static void mySelect()
  47.     {
  48.         try
  49.         {
  50.             myStatement = myConnection.createStatement();
  51.             String mySelect = "select * from mytable";
  52.             myResultSet = myStatement.executeQuery(mySelect);
  53.            
  54.             while (myResultSet.next())
  55.             {
  56.                 nome = myResultSet.getString("nome");
  57.                 cognome = myResultSet.getString("cognome");
  58.                 eta = myResultSet.getInt("eta");
  59.                 System.out.printf("\n%s - %s - %d\n", nome, cognome, eta);
  60.             }
  61.         }
  62.         catch (SQLException e)
  63.         {
  64.             System.out.println(e.getMessage());
  65.         }
  66.         finally
  67.         {
  68.             if (myResultSet != null)
  69.             {
  70.                 try
  71.                 {
  72.                     myResultSet.close();
  73.                 }
  74.                 catch (SQLException e)
  75.                 {
  76.                     System.out.println(e.getMessage());
  77.                 }
  78.             }
  79.             if (myStatement != null)
  80.             {
  81.                 try
  82.                 {
  83.                     myStatement.close();
  84.                 }
  85.                 catch (SQLException e)
  86.                 {
  87.                     System.out.println(e.getMessage());
  88.                 }
  89.             }
  90.             /*if (myConnection != null)
  91.             {
  92.                 try
  93.                 {
  94.                     myConnection.close();
  95.                 }
  96.                 catch (SQLException e)
  97.                 {
  98.                     System.out.println(e.getMessage());
  99.                 }
  100.             }*/
  101.         }
  102.     }
  103.    
  104.     public static void myInsert() throws SQLException
  105.     {
  106.         String nome = "Marc";
  107.         String cognome = "Augé";
  108.         int eta = 62;
  109.         String myInsert = "insert into mytable (nome, cognome, eta) values (?, ?, ?)";
  110.         myPreparedStatement = myConnection.prepareStatement(myInsert);
  111.        
  112.         myPreparedStatement.setString(1, nome);
  113.         myPreparedStatement.setString(2, cognome);
  114.         myPreparedStatement.setInt(3, eta);
  115.         myPreparedStatement.executeUpdate();
  116.        
  117.         System.out.printf("\n%s %s inserito.", nome, cognome);
  118.        
  119.         myPreparedStatement.close();
  120.     }
  121.    
  122.     public static void myUpdate() throws SQLException
  123.     {
  124.         String nome1 = "Marc";
  125.         String nome2 = "Mark";
  126.         String myUpdate = "update mytable set nome=? where nome=?";
  127.        
  128.         myPreparedStatement = myConnection.prepareStatement(myUpdate);
  129.         myPreparedStatement.setString(1, nome2);
  130.         myPreparedStatement.setString(2, nome1);
  131.         myConnection.setAutoCommit(false);
  132.         myPreparedStatement.executeUpdate();
  133.         myConnection.commit();
  134.        
  135.         System.out.printf("\nRecord aggiornato: %s => %s", nome1, nome2);
  136.     }
  137.    
  138.     public static void myDelete() throws SQLException
  139.     {
  140.         String nome = "Mark";
  141.         String myDelete = "delete from mytable where nome=?";
  142.        
  143.         myPreparedStatement = myConnection.prepareStatement(myDelete);
  144.         myPreparedStatement.setString(1, nome);
  145.         myPreparedStatement.executeUpdate();
  146.    
  147.         myPreparedStatement.close();
  148.        
  149.         System.out.println("\nRecord eliminato.");
  150.     }
  151.    
  152.     public static void myStoredUpdate() throws SQLException
  153.     {
  154.         myCallableStatement = myConnection.prepareCall("{call myProcedureUpdate(?,?)}");
  155.         myCallableStatement.setString(1, "Giovanni");
  156.         myCallableStatement.setString(2, "Zygmunt");
  157.        
  158.         System.out.println("\nProcedura di UPDATE eseguita.");
  159.     }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment