Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- /**
- * Created by Andrea on 4/10/16.
- */
- class DatabaseAccess {
- protected String jdbcDriver = "org.gjt.mm.mysql.Driver";
- protected String jdbcURL = "jdbc:mysql://localhost/FinalProject";
- protected String userName = "root";
- protected String password = "";
- // Connection
- protected Connection connection = null;
- public static void main(String[] args) {
- DatabaseAccess access = new DatabaseAccess();
- access.initConnection();
- access.query();
- }
- protected void initConnection(){
- try {
- Class.forName(jdbcDriver).newInstance();
- connection = DriverManager.getConnection(jdbcURL,userName,password);
- }
- catch (SQLException e){
- System.out.println(e.getMessage());
- }
- catch (ClassNotFoundException e){
- System.out.println(e.getMessage());
- }
- catch (InstantiationException e){
- System.out.println(e.getMessage());
- }
- catch (IllegalAccessException e){
- System.out.println(e.getMessage());
- }
- }
- private void query(){
- try {
- String query = "" +
- "SELECT * " +
- "FROM EMPLOYEE " +
- "WHERE SALARY >= 30000";
- Statement statement = connection.createStatement();
- ResultSet resultSet = statement.executeQuery(query);
- while (resultSet.next()){
- String fname = resultSet.getString(1);
- String mname = resultSet.getString(2);
- String lname = resultSet.getString(3);
- String salary = resultSet.getString(8);
- System.out.print("["+fname+"\t"+mname+"\t"+lname+"\t"+salary+"]");
- }
- // connection.close();
- // resultSet.close();
- }
- catch (SQLException e){
- System.out.println(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement