Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab2;
- import java.io.Console;
- import java.sql.*;
- import java.util.Scanner;
- public class JavaMysqlSelectExample {
- public static void main(String[] args) {
- try
- {
- // create the mysql database connection
- String myDriver = "com.mysql.jdbc.Driver";
- //specify the database to connect
- String myUrl = "jdbc:mysql://localhost/studentcontact";
- //connect to the database
- Class.forName(myDriver);
- Connection conn = DriverManager.getConnection(myUrl, "root", "");
- // create the SQL select statement.
- String user = "SELECT * FROM user";
- String item = "SELECT * FROM item";
- String returnBorrow = "SELECT * FROM returnborrow";
- // create the java statement
- Statement st = conn.createStatement();
- // st.executeUpdate("INSERT INTO item VALUES (6, 'Alice in Wonderland',225 , 'True')");
- st.executeUpdate("DELETE FROM item WHERE itemId = 5");
- // execute the query; the result of the execution is stored in an object of ResultSet class
- ResultSet rs = st.executeQuery(item);
- // there can be more than one result. so, we have to go through each data. in each round, only one record will be readed.
- while (rs.next())
- {
- System.out.println(rs.getString("itemName"));
- }
- st.close();
- }
- catch (Exception e)
- {
- System.err.println("Got an exception! ");
- System.err.println(e.getMessage());
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement