Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.DriverManager;
- import java.sql.Connection;
- import java.sql.Statement;
- import java.sql.PreparedStatement;
- import java.sql.CallableStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class MyJDBC
- {
- private static String url = "jdbc:mysql://localhost:3306/mydb";
- private static String user = "root";
- private static String psw = "0_mysql_1";
- private static Connection myConnection = null;
- private static Statement myStatement = null;
- private static PreparedStatement myPreparedStatement = null;
- private static CallableStatement myCallableStatement = null;
- private static ResultSet myResultSet = null;
- private static String nome;
- private static String cognome;
- private static int eta;
- public static void main(String[] args)
- {
- try
- {
- myConnection = DriverManager.getConnection(url, user, psw);
- System.out.println("Connessione al db riuscita.\n");
- myStoredUpdate();
- mySelect();
- }
- catch (SQLException e)
- {
- System.out.printf("Errore: %s", e.getMessage());
- }
- }
- // CRUD - Create, Read, Update, Delete
- // SELECT
- public static void mySelect()
- {
- try
- {
- myStatement = myConnection.createStatement();
- String mySelect = "select * from mytable";
- myResultSet = myStatement.executeQuery(mySelect);
- while (myResultSet.next())
- {
- nome = myResultSet.getString("nome");
- cognome = myResultSet.getString("cognome");
- eta = myResultSet.getInt("eta");
- System.out.printf("\n%s - %s - %d\n", nome, cognome, eta);
- }
- }
- catch (SQLException e)
- {
- System.out.println(e.getMessage());
- }
- finally
- {
- if (myResultSet != null)
- {
- try
- {
- myResultSet.close();
- }
- catch (SQLException e)
- {
- System.out.println(e.getMessage());
- }
- }
- if (myStatement != null)
- {
- try
- {
- myStatement.close();
- }
- catch (SQLException e)
- {
- System.out.println(e.getMessage());
- }
- }
- /*if (myConnection != null)
- {
- try
- {
- myConnection.close();
- }
- catch (SQLException e)
- {
- System.out.println(e.getMessage());
- }
- }*/
- }
- }
- public static void myInsert() throws SQLException
- {
- String nome = "Marc";
- String cognome = "Augé";
- int eta = 62;
- String myInsert = "insert into mytable (nome, cognome, eta) values (?, ?, ?)";
- myPreparedStatement = myConnection.prepareStatement(myInsert);
- myPreparedStatement.setString(1, nome);
- myPreparedStatement.setString(2, cognome);
- myPreparedStatement.setInt(3, eta);
- myPreparedStatement.executeUpdate();
- System.out.printf("\n%s %s inserito.", nome, cognome);
- myPreparedStatement.close();
- }
- public static void myUpdate() throws SQLException
- {
- String nome1 = "Marc";
- String nome2 = "Mark";
- String myUpdate = "update mytable set nome=? where nome=?";
- myPreparedStatement = myConnection.prepareStatement(myUpdate);
- myPreparedStatement.setString(1, nome2);
- myPreparedStatement.setString(2, nome1);
- myConnection.setAutoCommit(false);
- myPreparedStatement.executeUpdate();
- myConnection.commit();
- System.out.printf("\nRecord aggiornato: %s => %s", nome1, nome2);
- }
- public static void myDelete() throws SQLException
- {
- String nome = "Mark";
- String myDelete = "delete from mytable where nome=?";
- myPreparedStatement = myConnection.prepareStatement(myDelete);
- myPreparedStatement.setString(1, nome);
- myPreparedStatement.executeUpdate();
- myPreparedStatement.close();
- System.out.println("\nRecord eliminato.");
- }
- public static void myStoredUpdate() throws SQLException
- {
- myCallableStatement = myConnection.prepareCall("{call myProcedureUpdate(?,?)}");
- myCallableStatement.setString(1, "Giovanni");
- myCallableStatement.setString(2, "Zygmunt");
- System.out.println("\nProcedura di UPDATE eseguita.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment