SHARE
TWEET

Untitled

a guest Sep 22nd, 2016 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package lab2;
  2.  
  3. import java.io.Console;
  4. import java.sql.*;
  5. import java.util.Scanner;
  6. public class JavaMysqlSelectExample {
  7.     public static void main(String[] args) {
  8.    
  9.         try
  10.         {          
  11.           // create the mysql database connection
  12.           String myDriver = "com.mysql.jdbc.Driver";
  13.          
  14.           //specify the database to connect
  15.           String myUrl = "jdbc:mysql://localhost/studentcontact";
  16.          
  17.           //connect to the database
  18.           Class.forName(myDriver);
  19.           Connection conn = DriverManager.getConnection(myUrl, "root", "");
  20.          
  21.           // create the SQL select statement.
  22.           String user = "SELECT * FROM user";
  23.           String item = "SELECT * FROM item";
  24.           String returnBorrow = "SELECT * FROM returnborrow";
  25.  
  26.           // create the java statement
  27.           Statement st = conn.createStatement();
  28. //        st.executeUpdate("INSERT INTO item VALUES (6, 'Alice in Wonderland',225 , 'True')");
  29.           st.executeUpdate("DELETE FROM item WHERE itemId = 5");
  30.          
  31.           // execute the query; the result of the execution is stored in an object of ResultSet class
  32.           ResultSet rs = st.executeQuery(item);
  33.          
  34.          
  35.          
  36.           // there can be more than one result. so, we have to go through each data. in each round, only one record will be readed.
  37.          
  38.               while (rs.next())
  39.               {
  40.                   System.out.println(rs.getString("itemName"));
  41.               }
  42.          
  43.           st.close();
  44.         }
  45.        
  46.         catch (Exception e)
  47.         {
  48.           System.err.println("Got an exception! ");
  49.           System.err.println(e.getMessage());
  50.           e.printStackTrace();
  51.         }
  52.  
  53.     }
  54.    
  55. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top