package com import java sql public class Main static final String JDBC

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
package com;
import java.sql.*;
public class Main {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://10.64.6.9/university";
static final String USER = "st_fcit";
static final String PASS = "111111";
public static void main(String s[]){
Connection conn = null;
Statement stmtInsert = null;
Statement stmtCreate = null;
Statement stmtDrop = null;
Statement stmtRead = null;
PreparedStatement parametersInsert;
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmtDrop.executeUpdate("DROP TABLE user;");
System.out.println("Creating statement...");
stmtCreate = conn.createStatement();
String commandCreate = "CREATE TABLE user " +
"(id int not NULL, " +
" name VARCHAR(50), " +
" telephone VARCHAR(50), " +
" PRIMARY KEY ( id ))";
stmtCreate.executeUpdate(commandCreate);
String commandDrop = "DROP TABLE user;";
stmtDrop.executeUpdate(commandDrop);
/*
String name = "Andriy";
String phone = "1234567890";
parametersInsert =conn.prepareStatement("INSERT INTO user(name,telephone) VALUES (?, ?)");
parametersInsert.setString(1, name);
parametersInsert.setString(2, phone);
parametersInsert.executeUpdate();
String sql;
sql = "SELECT * FROM user";
ResultSet rs = stmtRead.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
String name1 = rs.getString("name");
String telephone = rs.getString("telephone");
//Display values
System.out.println(" name: " + name1);
System.out.println(" telephone: " + telephone);
}
String commandDrop = "DROP TABLE user;";
stmtDrop.executeUpdate(commandDrop);
/*
String sql;
sql = "SELECT * FROM curator";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
String personID = rs.getString("personID");
String curatorID = rs.getString("curatorID");
//Display values
System.out.println(" personID: " + personID);
System.out.println(" curatorID: " +curatorID);
}
*/
} catch (Exception ex) {
// handle the error
System.out.print("Exception : " + ex.getMessage());
}
System.out.println("^_^");
}
}