· 8 years ago · Sep 21, 2017, 08:10 PM
1package cs360;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.PreparedStatement;
6import java.sql.ResultSet;
7import java.sql.Statement;
8import java.util.Properties;
9
10/**
11 *
12 * @author mou1609
13 */
14
15public class Cs360 {
16
17 public static void main(String[] args) {
18 final String user = "root";
19 final String password = "root";
20 try {
21 Class.forName("com.mysql.jdbc.Driver");
22 Properties info = new Properties();
23 info.put("user", user);
24 info.put("password", password);
25 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cs360", info);
26 PreparedStatement dropTable = con.prepareStatement(
27 "DROP TABLE IF EXISTS Employee;");
28 dropTable.executeUpdate();
29 PreparedStatement createTable = con.prepareStatement(
30 "CREATE TABLE Employee (" +
31 "Id INT NOT NULL AUTO_INCREMENT," +
32 "Name VARCHAR(200)," +
33 "Salary INT," +
34 "DepartmentId INT," +
35 "PRIMARY KEY (Id)" +
36 ");");
37 createTable.executeUpdate();
38 PreparedStatement preparedStatement = con.prepareStatement(
39 "INSERT INTO Employee (Name, Salary, DepartmentId) VALUES (?, ?, ?),(?, ?, ?)");
40 preparedStatement.setString(1, "Joe");
41 preparedStatement.setInt(2, 70000);
42 preparedStatement.setInt(3, 1);
43 preparedStatement.setString(4, "Henry");
44 preparedStatement.setInt(5, 80000);
45 preparedStatement.setInt(6, 2);
46 preparedStatement.executeUpdate();
47
48 Statement stmt = con.createStatement();
49 ResultSet rs = stmt.executeQuery("select * from Employee");
50 while (rs.next()) {
51 System.out.println(rs.getInt("Id") + " "
52 + rs.getString("Name") + " "
53 + rs.getInt("Salary") + " "
54 + rs.getInt("DepartmentId"));
55 }
56 con.close();
57 } catch (Exception e) {
58 System.out.println(e);
59 }
60 }
61
62}