Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cs360;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import java.util.Properties;
- /**
- *
- * @author mou1609
- */
- public class Cs360 {
- public static void main(String[] args) {
- final String user = "root";
- final String password = "root";
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Properties info = new Properties();
- info.put("user", user);
- info.put("password", password);
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cs360", info);
- PreparedStatement dropTable = con.prepareStatement(
- "DROP TABLE IF EXISTS Employee;");
- dropTable.executeUpdate();
- PreparedStatement createTable = con.prepareStatement(
- "CREATE TABLE Employee (" +
- "Id INT NOT NULL AUTO_INCREMENT," +
- "Name VARCHAR(200)," +
- "Salary INT," +
- "DepartmentId INT," +
- "PRIMARY KEY (Id)" +
- ");");
- createTable.executeUpdate();
- PreparedStatement preparedStatement = con.prepareStatement(
- "INSERT INTO Employee (Name, Salary, DepartmentId) VALUES (?, ?, ?),(?, ?, ?)");
- preparedStatement.setString(1, "Joe");
- preparedStatement.setInt(2, 70000);
- preparedStatement.setInt(3, 1);
- preparedStatement.setString(4, "Henry");
- preparedStatement.setInt(5, 80000);
- preparedStatement.setInt(6, 2);
- preparedStatement.executeUpdate();
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("select * from Employee");
- while (rs.next()) {
- System.out.println(rs.getInt("Id") + " "
- + rs.getString("Name") + " "
- + rs.getInt("Salary") + " "
- + rs.getInt("DepartmentId"));
- }
- con.close();
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement