Advertisement
Guest User

sql

a guest
Sep 7th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. package Main;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.Vector;
  9.  
  10.  
  11. public class Tester {
  12.     final static String URL="jdbc:mysql://127.0.0.1:3306/";
  13.     final static String DB_NAME="hackeru";
  14.     final static String USER_NAME="root";
  15.     final static String USER_PASS="";
  16.     private static Connection db;
  17.     private static PreparedStatement stmt;
  18.     private static Vector <myPhones> myVec = new Vector<>();
  19.    
  20.     public static void main(String[] args) throws Exception {
  21.        
  22.    
  23.         //CREATING A CONCTION TO OUR DATABASE
  24.         db=DriverManager.getConnection(URL+DB_NAME,USER_NAME,USER_PASS);
  25.         //createTable();
  26.         //setCode();
  27.         //setCliume();
  28.         getData();
  29.         System.out.println("=================================================");
  30.         for(int i=0;i<myVec.size();i+=1){
  31.             if(myVec.get(i).getQey()>0){
  32.                 System.out.println(myVec.get(i).toString());
  33.             }
  34.                
  35.         }
  36.     }
  37.     /*private static void setCliume(){
  38.         String sql="ALTER TABLE PHONES ADD qey INT DEFAULT 0 ";
  39.        
  40.        
  41.        
  42.         try {
  43.             //PREPARE STATMENT FOR GIVEN SQL
  44.             stmt=db.prepareStatement(sql);
  45.            
  46.             //execute the statment
  47.             stmt.execute();
  48.         } catch (SQLException e) {
  49.            
  50.             e.printStackTrace();
  51.         }
  52.     }*/
  53.     private static void setCode(){
  54.         //create a SQL command
  55.                 String sql="INSERT INTO phones(name,price,qey) values ('Samsung note 8','3500','3'),('MeiZu','1800','0'),"
  56.                            +"('Iphone 7','3900','5'),('LG g8','3000','2'),('Asus Zen 8','3600','4'),('Xiami noteb 6','3700','1')";
  57.                        
  58.                        
  59.                
  60.                 try {
  61.                     //PREPARE STATMENT FOR GIVEN SQL
  62.                     stmt=db.prepareStatement(sql);
  63.                    
  64.                     //execute the statment
  65.                     stmt.execute();
  66.                 } catch (SQLException e) {
  67.                    
  68.                     e.printStackTrace();
  69.                 }
  70.                 System.out.println("Table wase created");
  71.     }
  72.     /*private static void createTable() {
  73.         //create a SQL command
  74.         String sql="CREATE TABLE IF NOT EXISTS phones"
  75.                 +"(id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(16) NOT NULL ,price INT DEFAULT 0);";
  76.                
  77.        
  78.         try {
  79.             //PREPARE STATMENT FOR GIVEN SQL
  80.             stmt=db.prepareStatement(sql);
  81.            
  82.             //execute the statment
  83.             stmt.execute();
  84.         } catch (SQLException e) {
  85.            
  86.             e.printStackTrace();
  87.         }
  88.         System.out.println("Table wase created");
  89.     }*/
  90.     public static void getData() throws Exception{
  91.          //create sql statment
  92.         String sql="SELECT * FROM phones";
  93.         //get result from mySQL to an object called ResultSet
  94.         ResultSet result=db.prepareStatement(sql).executeQuery();
  95.         //System.out.println(result.toString());
  96.        
  97.         for (result.first();!result.isAfterLast();result.next())
  98.         {
  99.             myVec.add(new myPhones(
  100.                     result.getString("name"),
  101.                     result.getInt("price"),
  102.                     result.getInt("qey")
  103.                     ));
  104.         }
  105.         for (myPhones item:myVec)
  106.         {
  107.             System.out.println(item);
  108.         }
  109.     }
  110. }
  111. ======================
  112. package Main;
  113.  
  114. public class myPhones {
  115.    
  116.         public String name;
  117.         public int price;
  118.         public int qey;
  119.        
  120.         public myPhones(String name, int price, int qey) {
  121.             super();
  122.             this.name = name;
  123.             this.price = price;
  124.             this.qey = qey;
  125.         }
  126.      
  127.         public String getName() {
  128.             return name;
  129.         }
  130.  
  131.         public int getPrice() {
  132.             return price;
  133.         }
  134.  
  135.         public int getQey() {
  136.             return qey;
  137.         }
  138.  
  139.         @Override
  140.         public String toString() {
  141.             return "myPhones name=" + name + " price=" + price + " qty=" + qey +" value:"+price*qey;
  142.         }
  143.        
  144.        
  145.        
  146.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement