Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication2;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class JavaApplication2 {
- private final static String DBURL = "jdbc:mysql://localhost:1433";
- private final static String DBUSER = "ktos";
- private final static String DBPASS = "pass";
- private final static String DBDRIVER = "com.mysql.jdbc.Driver";
- private static Connection connection;
- private static Statement statement;
- public static void main(String[] args) {
- try{
- Class.forName(DBDRIVER);
- System.out.println("start");
- connection = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
- System.out.println("next");
- statement = connection.createStatement();
- String sql = "SELECT id, first, last, age FROM Employees";
- ResultSet rs = statement.executeQuery(sql);
- while(rs.next()){
- int id = rs.getInt("id");
- int age = rs.getInt("age");
- String first = rs.getString("first");
- String last = rs.getString("last");
- System.out.print("ID: " + id);
- System.out.print(", Age: " + age);
- System.out.print(", First: " + first);
- System.out.println(", Last: " + last);
- }
- //STEP 6: Clean-up environment
- rs.close();
- statement.close();
- connection.close();
- }catch(SQLException se){
- //Handle errors for JDBC
- se.printStackTrace();
- }catch(Exception e){
- //Handle errors for Class.forName
- e.printStackTrace();
- }finally{
- try{
- if(statement!=null) statement.close();
- }catch(SQLException se2){
- }
- try{
- if(connection!=null) connection.close();
- }catch(SQLException se){
- se.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement