Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.util.Scanner;
- public class main {
- static final String url = "jdbc:mysql://127.0.0.1:3306/?user=root&password=davide2015&useSSL=false";
- static final String driver = "com.mysql.jdbc.Driver";
- public static void main(String[] args) {
- try
- {
- Scanner in = new Scanner(System.in);
- Class.forName(driver);
- Connection conn = DriverManager.getConnection(url);
- for(SQLWarning prb = conn.getWarnings(); prb != null; prb = prb.getNextWarning())
- {
- System.out.println("PROBLEMA SQL:");
- System.out.println("STATO : " + prb.getSQLState());
- System.out.println("MESSAGGIO: " + prb.getMessage());
- System.out.println("ERRORE : " + prb.getErrorCode());
- }
- boolean done= false;
- int risp = 0;
- while(!done)
- {
- System.out.println("Quale Operazione eseguire?(Op.1 - Op.10)");
- String risposta = in.nextLine();
- risp = Integer.parseInt(risposta);
- if(risp >= 1 && risp <= 10)
- {
- done= true;
- }
- }
- if(risp==1){
- System.out.println("Op.1 Inserire un'attività nel parco\n");
- System.out.println("Inserisci codice e nome dell'attività");
- String codice = in.nextLine();
- String nome = in.nextLine();
- Statement stato = conn.createStatement();
- PreparedStatement pr = conn.prepareStatement("INSERT INTO Attività values (?,?)");
- pr.setString(1, codice);
- pr.setString(2, nome);
- pr.execute();
- ResultSet rs = stato.executeQuery("SELECT * FROM Attività");
- System.out.println(" codice nome \n");
- while (rs.next()){
- System.out.printf("%-8s",rs.getString(1));
- System.out.printf("%-13a",rs.getString(2));
- }
- rs.close();
- stato.close();
- pr.close();
- }
- }
- catch (Exception e)
- {
- System.out.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement