Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Main;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Vector;
- public class Tester {
- final static String URL="jdbc:mysql://127.0.0.1:3306/";
- final static String DB_NAME="hackeru";
- final static String USER_NAME="root";
- final static String USER_PASS="";
- private static Connection db;
- private static PreparedStatement stmt;
- private static Vector <myPhones> myVec = new Vector<>();
- public static void main(String[] args) throws Exception {
- //CREATING A CONCTION TO OUR DATABASE
- db=DriverManager.getConnection(URL+DB_NAME,USER_NAME,USER_PASS);
- //createTable();
- //setCode();
- //setCliume();
- getData();
- System.out.println("=================================================");
- for(int i=0;i<myVec.size();i+=1){
- if(myVec.get(i).getQey()>0){
- System.out.println(myVec.get(i).toString());
- }
- }
- }
- /*private static void setCliume(){
- String sql="ALTER TABLE PHONES ADD qey INT DEFAULT 0 ";
- try {
- //PREPARE STATMENT FOR GIVEN SQL
- stmt=db.prepareStatement(sql);
- //execute the statment
- stmt.execute();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }*/
- private static void setCode(){
- //create a SQL command
- String sql="INSERT INTO phones(name,price,qey) values ('Samsung note 8','3500','3'),('MeiZu','1800','0'),"
- +"('Iphone 7','3900','5'),('LG g8','3000','2'),('Asus Zen 8','3600','4'),('Xiami noteb 6','3700','1')";
- try {
- //PREPARE STATMENT FOR GIVEN SQL
- stmt=db.prepareStatement(sql);
- //execute the statment
- stmt.execute();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- System.out.println("Table wase created");
- }
- /*private static void createTable() {
- //create a SQL command
- String sql="CREATE TABLE IF NOT EXISTS phones"
- +"(id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(16) NOT NULL ,price INT DEFAULT 0);";
- try {
- //PREPARE STATMENT FOR GIVEN SQL
- stmt=db.prepareStatement(sql);
- //execute the statment
- stmt.execute();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- System.out.println("Table wase created");
- }*/
- public static void getData() throws Exception{
- //create sql statment
- String sql="SELECT * FROM phones";
- //get result from mySQL to an object called ResultSet
- ResultSet result=db.prepareStatement(sql).executeQuery();
- //System.out.println(result.toString());
- for (result.first();!result.isAfterLast();result.next())
- {
- myVec.add(new myPhones(
- result.getString("name"),
- result.getInt("price"),
- result.getInt("qey")
- ));
- }
- for (myPhones item:myVec)
- {
- System.out.println(item);
- }
- }
- }
- ======================
- package Main;
- public class myPhones {
- public String name;
- public int price;
- public int qey;
- public myPhones(String name, int price, int qey) {
- super();
- this.name = name;
- this.price = price;
- this.qey = qey;
- }
- public String getName() {
- return name;
- }
- public int getPrice() {
- return price;
- }
- public int getQey() {
- return qey;
- }
- @Override
- public String toString() {
- return "myPhones name=" + name + " price=" + price + " qty=" + qey +" value:"+price*qey;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement