Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package destinatii_turistice;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class MainApp {
- public static void main(String[] args) throws IOException, IllegalAccessException,InstantiationException,SQLException,ClassNotFoundException {
- // TODO Auto-generated method stub
- String url="jdbc:mysql://localhost:3306/test";
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection con=DriverManager.getConnection(url,"root","root");
- BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
- int opt;
- do
- {
- System.out.println("1.Afisare in consola a datelor din MySql");
- System.out.println("2.Adaugare in tabela a unei destinatiii citite de la consola");
- System.out.println("3.Actualizare nr obiective turistice pentru o statiune citita");
- System.out.println("Dati optiune : ");
- opt=Integer.parseInt(reader.readLine());
- switch(opt)
- {
- case 1:
- Statement sql = (Statement)con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
- ResultSet rs=sql.executeQuery("Select *from destinatii");
- while(rs.next())
- {
- System.out.println(rs.getString("tara")+" "+rs.getString("statiune")+" "+rs.getInt("nr_obiective"));
- }
- System.out.println("Am afisat!!!!");
- sql.close();
- rs.close();
- break;
- case 2:
- Statement sql1 = (Statement)con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
- ResultSet rs1=sql1.executeQuery("Select *from destinatii");
- String tara1;
- String statiune1;
- int nr_obiective1;
- int index1;
- System.out.println("Dati tara de adaugat : ");
- tara1=reader.readLine();
- System.out.println("Dati statiune de adaugat : ");
- statiune1=reader.readLine();
- System.out.println("Dati nr de obiective : ");
- nr_obiective1=Integer.parseInt(reader.readLine());
- /*System.out.println("Dati indexul: ");
- index1=Integer.parseInt(reader.readLine());*/
- rs1.moveToInsertRow();
- rs1.updateString("tara", tara1);
- rs1.updateString("statiune", statiune1);
- rs1.updateInt("nr_obiective",nr_obiective1);
- rs1.insertRow();
- sql1.close();
- rs1.close();
- break;
- case 3:
- Statement sql2 = (Statement)con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
- String statiune11;
- int nr_obiective11;
- System.out.println("Dati statiune pentru update : ");
- statiune11=reader.readLine();
- System.out.println("Dati nr de obiective : ");
- nr_obiective11=Integer.parseInt(reader.readLine());
- ResultSet rs2=sql2.executeQuery("Select *from destinatii where statiune=\""+statiune11 + "\"");
- rs2.first();
- rs2.updateInt("nr_obiective", nr_obiective11);
- rs2.updateRow();
- sql2.close();
- rs2.close();
- break;
- //sterge
- case 4:
- Statement sql3 = (Statement)con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
- String statiune3;
- int nr_obiective3;
- System.out.println("Dati statiune pentru sters : ");
- statiune3=reader.readLine();
- ResultSet rs3=sql3.executeQuery("Select *from destinatii where statiune=\""+statiune3 + "\"");
- rs3.first();
- rs3.deleteRow();
- sql3.close();
- rs3.close();
- break;
- }
- }while(opt<5);
- con.close();
- }
- }
- package destinatii_turistice;
- public class Destinatie {
- private String tara;
- private String statiune;
- private int nr_obiective;
- public Destinatie(String tara, String statiune, int nr_obiective) {
- this.tara = tara;
- this.statiune = statiune;
- this.nr_obiective = nr_obiective;
- }
- public String getTara() {
- return tara;
- }
- public void setTara(String tara) {
- this.tara = tara;
- }
- public String getStatiune() {
- return statiune;
- }
- public void setStatiune(String statiune) {
- this.statiune = statiune;
- }
- public int getNr_obiective() {
- return nr_obiective;
- }
- public void setNr_obiective(int nr_obiective) {
- this.nr_obiective = nr_obiective;
- }
- @Override
- public String toString() {
- return "tara=" + tara + ", statiune=" + statiune
- + ", nr_obiective=" + nr_obiective + "]";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement