Display connection mysql workbench
Install : https://www.youtube.com/watch?v=V8F-RxzI_2w
1- Insert /2-Delete/3-Display/ 4-Exit
3
Connecting to database...
ID: 1, Name: Dai, Class: cl, Age: 13
ID: 3, Name: Dang, Class: cl, Age: 20
ID: 4, Name: Tho, Class: cl, Age: 28
ID: 6, Name: Long, Class: cl, Age: 6
ID: 8, Name: Link, Class: po, Age: 22
ID: 9, Name: Lin, Class: cl, Age: 22
ID: 10, Name: Huynh, Class: po, Age: 24
ID: 11, Name: Phung, Class: po, Age: 24
ID: 12, Name: Yen, Class: po, Age: 24
ID: 13, Name: y, Class: po, Age: 22
ID: 14, Name: Long, Class: po, Age: 24
Install : https://www.youtube.com/watch?v=V8F-RxzI_2w
1- Insert /2-Delete/3-Display/ 4-Exit
3
Connecting to database...
ID: 1, Name: Dai, Class: cl, Age: 13
ID: 3, Name: Dang, Class: cl, Age: 20
ID: 4, Name: Tho, Class: cl, Age: 28
ID: 6, Name: Long, Class: cl, Age: 6
ID: 8, Name: Link, Class: po, Age: 22
ID: 9, Name: Lin, Class: cl, Age: 22
ID: 10, Name: Huynh, Class: po, Age: 24
ID: 11, Name: Phung, Class: po, Age: 24
ID: 12, Name: Yen, Class: po, Age: 24
ID: 13, Name: y, Class: po, Age: 22
ID: 14, Name: Long, Class: po, Age: 24
Example:
package aptech;
public class Test {
public static void main(String[] args) {
DataConsole dc = new DataConsole();
dc.start();
}
}
package aptech;
import java.util.Scanner;
public class DataConsole {
public void start() {
while (true) {
int choice = menu();
switch (choice) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
System.out.println("GoodBye!");
System.exit(0);
}
}
}
public int menu() {
System.out.println("\n1- Insert /2-Delete/3-Display/ 4-Exit");
int choice = readln();
return choice;
}
Scanner s = new Scanner(System.in);
public int readln() {
int n = 0;
try {
n = Integer.parseInt(s.nextLine());
} catch (Exception e) {
// TODO: handle exception
}
return n;
}
DataManager control = new DataManager();
public void insert() {
control.insert();
}
public void delete() {
control.delete();
}
public void display() {
control.display();
}
}
package aptech; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; public class DataManager { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/simple"; // Database credentials static final String USER = "root"; static final String PASS = "1234567"; public void insert() { Connection conn = null; Statement stmt = null; try { // STEP 2: Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); // STEP 3: Open a connection System.out.println("Connecting to a selected database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); System.out.println("Connected database successfully..."); // STEP 4: Execute a query System.out.println("Inserting records into the table..."); stmt = conn.createStatement(); Scanner scanner = new Scanner(System.in); String yes = ""; do { System.out.print("Name: "); String name = scanner.nextLine(); System.out.print("Class: "); String clas = scanner.nextLine(); System.out.print("Age: "); int age = Integer.parseInt(scanner.nextLine()); String sql = "INSERT INTO `simple`.`person` (`Name`, `class`, `age`) VALUES ('" + name + "', '" + clas + "', '" + age + "')"; System.out.println("Sql: " + sql); stmt.executeUpdate(sql); System.out.println("Inserted records into the table..."); System.out.println("Exit [y/n]"); yes = scanner.nextLine(); } while (yes.compareTo("y") != 0); } catch (SQLException se) { // Handle errors for JDBC // se.printStackTrace(); } catch (Exception e) { // Handle errors for Class.forName // e.printStackTrace(); } finally { // finally block used to close resources try { if (stmt != null) conn.close(); } catch (SQLException se) { } // do nothing try { if (conn != null) conn.close(); } catch (SQLException se) { // se.printStackTrace(); } // end finally try } // end try } public void delete() { Connection conn = null; Statement stmt = null; try { // STEP 2: Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); // STEP 3: Open a connection System.out.println("Connecting to a selected database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); System.out.println("Connected database successfully..."); // STEP 4: Execute a query System.out.println("Inserting records into the table..."); stmt = conn.createStatement(); Scanner scanner = new Scanner(System.in); String yes = ""; do { System.out.print("Number ID: "); String id = scanner.nextLine(); String sql = "DELETE FROM `simple`.`person` WHERE `ID`='"+id+"'"; System.out.println("Sql: " + sql); stmt.executeUpdate(sql); System.out.println("DELETE records into the table..."); System.out.println("Exit [y/n]"); yes = scanner.nextLine(); } while (yes.compareTo("y") != 0); } catch (SQLException se) { // Handle errors for JDBC // se.printStackTrace(); } catch (Exception e) { // Handle errors for Class.forName // e.printStackTrace(); } finally { // finally block used to close resources try { if (stmt != null) conn.close(); } catch (SQLException se) { } // do nothing try { if (conn != null) conn.close(); } catch (SQLException se) { // se.printStackTrace(); } // end finally try } // end try } public void display() { // TODO Auto-generated method stub Connection conn = null; Statement stmt = null; try { // STEP 2: Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); // STEP 3: Open a connection System.out.println("Connecting to database..."); conn = (Connection) DriverManager.getConnection(DB_URL, USER, PASS); // STEP 4: Execute a query System.out.println("Creating statement..."); stmt = (Statement) conn.createStatement(); String sql = "SELECT ID, Name, class, age FROM person WHERE age<30;"; ResultSet rs = stmt.executeQuery(sql); // STEP 5: Extract data from result set while (rs.next()) { // Retrieve by column name int id = rs.getInt("ID"); String name = rs.getString("Name"); String clas = rs.getString("class"); int age = rs.getInt("age"); // Display values System.out.print("\nID: " + id); System.out.print(", Name: " + name); System.out.print(", Class: " + clas); System.out.print(", Age: " + age); } // STEP 6: Clean-up environment rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { // Handle errors for JDBC se.printStackTrace(); } catch (Exception e) { // Handle errors for Class.forName e.printStackTrace(); } finally { // finally block used to close resources try { if (stmt != null) stmt.close(); } catch (SQLException se2) { } // nothing we can do try { if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } // end finally try } // end try } }
Connection Database mysql Xampl + eclip
https://www.youtube.com/watch?v=4PeuMjDNYhI
0 nhận xét:
Post a Comment