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;
- import org.json.JSONArray;
- import org.json.JSONObject;
- public class Tester{
- private static Connection db;
- private static PreparedStatement stmt;
- 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 String aBrandName[]= {"ZTE","Nokia","OPO","HTC"};
- //private static int aPrice[] = {900,2500,2900,3250};
- //private static int aQty[] = {5,3,2,7};
- public static void main(String[] args) throws Exception {
- // creating our connection string
- // creating a connection to our database
- db = DriverManager.getConnection(URL + DB_NAME, USER_NAME, USER_PASS);
- //create table for the first time, and if the table exists do not create it....
- //createTable();
- //insert first data to our mySQL with JDBC
- //insertFirstDemo();
- //insert data in secured way.... (no SQL injection)
- //saferInsert();
- //insert array of data....
- /*
- for (int counter=0;counter<aBrandName.length;counter+=1)
- {
- saferInsert2(aBrandName[counter], aPrice[counter], aQty[counter]);
- }
- System.out.println("I was finished, yeah MAN!");
- */
- getData();
- }
- private 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());
- Vector <myPhones> myVec = new Vector<>();
- for (result.first();!result.isAfterLast();result.next())
- {
- myVec.add(new myPhones(
- result.getString("name"),
- result.getInt("price"),
- result.getInt("qty")
- ));
- }
- for (myPhones item:myVec)
- {
- System.out.println(item);
- }
- /*
- JSONArray jsonResult = new JSONArray();
- for (result.first();!result.isAfterLast();result.next())
- {
- jsonResult.put(new JSONObject()
- .put("name", result.getString("name"))
- .put("price", result.getInt("price"))
- .put("qty", result.getInt("qty"))
- );
- }
- System.out.println(jsonResult.toString());
- */
- }
- private static void saferInsert2(String brandName, int price, int qty) throws Exception
- {
- //assume there is a table phones with name,price,qty 1 2 3
- String sql="INSERT INTO phones (name,price,qty) VALUES (?,?,?)";
- PreparedStatement stmt = db.prepareStatement(sql); //prepare statement with 3 parms
- stmt.setString(1, brandName); //bind string param as first param (first question mark)
- stmt.setInt(2, price); //bind int param as second param (second question mark)
- stmt.setInt(3, qty); //bin int param as third param (becuase there is no f*** way that i will buy again apple, me want android
- stmt.execute();
- }
- private static void saferInsert() throws Exception
- {
- //assume there is a table phones with name,price,qty 1 2 3
- String sql="INSERT INTO phones (name,price,qty) VALUES (?,?,?)";
- PreparedStatement stmt = db.prepareStatement(sql); //prepare statement with 3 parms
- stmt.setString(1, "Iphone 7 plus"); //bind string param as first param (first question mark)
- stmt.setInt(2, 4300); //bind int param as second param (second question mark)
- stmt.setInt(3, 1); //bin int param as third param (becuase there is no f*** way that i will buy again apple, me want android
- stmt.execute();
- }
- private static void insertFirstDemo() {
- // create a SQL command.
- String sql = "INSERT INTO chocos (name) "
- + "values ('Para'),('Milka'),('Oreo'),('Bueno'),"
- + "('Ferror Roche');";
- try {
- // prepare statement for given SQL
- stmt = db.prepareStatement(sql);
- // execute the statement
- stmt.execute();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("Data was inserted");
- }
- private static void createTable() {
- // create a SQL command.
- String sql = "CREATE TABLE IF NOT EXISTS chocos" + " (id INT PRIMARY KEY AUTO_INCREMENT,"
- + " name VARCHAR (16) NOT NULL);";
- try {
- // prepare statement for given SQL
- stmt = db.prepareStatement(sql);
- // execute the statement
- stmt.execute();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("Table was created");
- }
- private static String strValues(String[] strs)
- {
- StringBuilder values = new StringBuilder();
- char del = ' ';
- for (String str:strs)
- {
- values.append(del).append("('").append(str).append("')");
- if (del == ' ') del=',';
- }
- return values.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement