Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package alcon;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- *
- * @author Leonardo
- */
- public class AndreDB {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- BufferedReader in;
- in = new BufferedReader(new InputStreamReader(System.in));
- try {
- int opcao = Integer.parseInt(in.readLine()); //captura a entrada feita pelo teclado, tem que ser numero
- while (opcao != 0) {
- switch (opcao) {
- case 0: System.exit(0);
- break;
- case 1:
- insereMarca();
- break;
- case 2:
- telemoveisTiagoSousa();
- break;
- case 3:
- insereMarcaOnePlus();
- break;
- case 4:
- printiPhone7();
- default:
- System.out.println("Insira alguma opção válida");
- break;
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- private static void insereMarca() {
- String driver = "com.mysql.jdbc.Driver";
- String dbname = "telemoveis";
- String dbuser = "andre";
- String password = "andre";
- String url = "jdbc:mysql://localhost:3306/" + dbname + "?useSSL=false";
- try {
- Class.forName(driver);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.err.println("Cannot load driver " + e.getMessage());
- System.exit(1);
- }
- try {
- BufferedReader in;
- in = new BufferedReader(new InputStreamReader(System.in));
- System.out.println("Insira uma nova marca");
- System.out.print(">> Nome: ");
- String nomeMarca = in.readLine();
- System.out.print(">> MoradaSede: ");
- String moradaSedeMarca = in.readLine();
- System.out.println("Nome: " + nomeMarca + " MoradaSede: " + moradaSedeMarca);
- Connection c = DriverManager.getConnection(url, dbuser, password);
- c.setAutoCommit(false);
- Statement stmt = c.createStatement();
- String sql = "INSERT INTO Marca(nome,moradaSede) VALUES('"
- + nomeMarca + "','" + moradaSedeMarca + "');";
- stmt.executeUpdate(sql);
- stmt.close();
- c.commit();
- c.close();
- System.out.println("Marca " + nomeMarca + " inserida.");
- } catch (Exception exx) {
- exx.printStackTrace();
- }
- }
- private static void telemoveisTiagoSousa() {
- String driver = "com.mysql.jdbc.Driver";
- String dbname = "telemoveis";
- String dbuser = "andre";
- String password = "andre";
- String url = "jdbc:mysql://localhost:3306/" + dbname + "?useSSL=false";
- try {
- Class.forName(driver);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.err.println("Cannot load driver " + e.getMessage());
- System.exit(1);
- }
- try {
- Connection c = DriverManager.getConnection(url, dbuser, password);
- c.setAutoCommit(false);
- Statement stmt = c.createStatement();
- String query = "SELECT modeloTelemovel, anoTelemovel "
- + "FROM TelemoveisClientes "
- + "WHERE nomeCliente='Tiago Sousa';";
- ResultSet rs = stmt.executeQuery(query);
- if (rs.next()) {
- String modeloT = rs.getString("modeloTelemovel");
- int anoT = rs.getInt("anoTelemovel");
- System.out.println("Modelo: " + modeloT);
- System.out.println("Ano: " + anoT);
- } else {
- System.out.println("Tiago Sousa não tem nenhum telemóvel");
- }
- rs.close();
- stmt.close();
- c.close();
- } catch (Exception e) {
- System.err.println(e.getClass().getName() + ": " + e.getMessage());
- System.exit(0);
- }
- }
- public class insereMarcaOnePlus {
- public static void main(String args[]) {
- String driver = "com.mysql.jdbc.Driver";
- String dbname = "telemoveis";
- String dbuser = "andre";
- String password = "andre";
- String url = "jdbc:mysql://localhost:3306/" + dbname + "?useSSL=false";
- try {
- Class.forName(driver);
- }
- catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.err.println("Cannot load driver " + e.getMessage());
- System.exit(1);
- }
- try {
- Connection c = DriverManager.getConnection(url, dbuser, password);
- c.setAutoCommit(false);
- Statement stmt = c.createStatement();
- String sql = "INSERT INTO marca(nome,moradaSede) VALUES('OnePlus', 'Shenzhen, China');";
- stmt.executeUpdate(sql);
- stmt.close();
- c.commit();
- c.close();
- System.out.println("Marca OnePlus inserida corretamente.");
- }
- catch (Exception e) {
- System.err.println( e.getClass().getName()+": "+ e.getMessage() );
- System.exit(0);
- }
- }
- public class printiPhone7 {
- public static void main(String args[]) {
- String driver = "com.mysql.jdbc.Driver";
- String dbname = "telemoveis";
- String dbuser = "andre";
- String password = "andre";
- String url = "jdbc:mysql://localhost:3306/" + dbname + "?useSSL=false";
- try {
- Class.forName(driver);
- }
- catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.err.println("Cannot load driver " + e.getMessage());
- System.exit(1);
- }
- try {
- Connection c = DriverManager.getConnection(url, dbuser, password);
- c.setAutoCommit(false);
- Statement stmt = c.createStatement();
- String query = "SELECT modelo, ano, pvp, tamanho, nomeMarca, modeloTelOrig, anoTelOrig " +
- "FROM telemoveis " +
- "WHERE nome='iPhone 7';";
- ResultSet rs = stmt.executeQuery(query);
- if( rs.next() ) {
- String modeloF = rs.getString("modelo");
- int anoF = rs.getInt("ano");
- int pvpF = rs.getInt("pvp");
- int tamanhoF = rs.getBoolean("tamanho");
- String nomeM = rs.getString("nomeMarca");
- String modeloTelOrigF = rs.getString("modeloTelOrig");
- int anoTelOrigF = rs.getString("anoTelOrig");
- System.out.println("Modelo: " + modeloF);
- System.out.println("Ano: " + anoF);
- System.out.println("PVP: " + pvpF);
- System.out.println("Tamanho: " + tamanhoF);
- System.out.println("Marca: " + nomeM);
- System.out.println("ModeloOriginal: " + modeloTelOrigF);
- System.out.println("AnoModeloOriginal: " + anoTelOrigF);
- }
- else
- System.out.println("iPhone 7 não foi encontrado na base de dados");
- rs.close();
- stmt.close();
- c.close();
- }
- catch (Exception e) {
- System.err.println( e.getClass().getName()+": "+ e.getMessage() );
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement