Guest User

Untitled

a guest
Mar 28th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. public static void runQuery(Connection connection){
  2. try{
  3. Statement stmt=con.createStatement();
  4. ResultSet rs=stmt.executeQuery("select * from table");
  5. while(rs.next()) {
  6. System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
  7. }
  8. }catch(Exception e){
  9. //Handle exception
  10. }
  11. }
  12. public static Connection getDBConnection(){
  13. Connection con = null;
  14. try{
  15. Class.forName("com.mysql.jdbc.Driver");
  16. con=DriverManager.getConnection( "jdbc:mysql://localhost:3306/db","user","password");
  17. }
  18. catch(Exception e){
  19. //Handle exception
  20. }
  21. return con;
  22. }
  23. public static void closeConnection(Connection connection){
  24. if(connection!=null && !connection.isClosed()){
  25. connection.close();
  26. }
  27. }
  28. public static void main(String[]args){
  29. Connection con = getDBConnection();
  30. runQuery(con);
  31. closeConnection(con);
  32. }
Add Comment
Please, Sign In to add comment