Advertisement
Guest User

Untitled

a guest
Mar 10th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. /**
  10. * Simple Java program to connect to MySQL database running on localhost and
  11. * running SELECT and INSERT query to retrieve and add data.
  12. *
  13. * @author Javin Paul
  14. */
  15. public class Main {
  16.  
  17. // JDBC URL, username and password of MySQL server
  18. private static final String url = "jdbc:mysql://db4free.net/lnmotest";
  19. private static final String user = "lnmotest";
  20. private static final String password = "lnmotest";
  21. private static final String url = "jdbc:mysql://192.168.1.159:3306/base";
  22. private static final String user = "chubakha";
  23. private static final String password = "chubakha";
  24.  
  25.  
  26. // JDBC variables for opening and managing connection
  27. private static Connection con;
  28. private static Statement stmt;
  29. private static ResultSet rs;
  30.  
  31. public static void main(String args[]) {
  32. String query = "select * from Users";
  33.  
  34. try {
  35. // opening database connection to MySQL server
  36. con = DriverManager.getConnection(url, user, password);
  37.  
  38. // getting Statement object to execute query
  39. stmt = con.createStatement();
  40.  
  41. // executing SELECT query
  42. rs = stmt.executeQuery(query);
  43.  
  44. while (rs.next()) {
  45. int id = rs.getInt("id");
  46. String login = rs.getString("login");
  47. String password = rs.getString("password");
  48. System.out.println("id: " + id + " login" + login + " password" + password);
  49. }
  50.  
  51. } catch (SQLException sqlEx) {
  52. sqlEx.printStackTrace();
  53. } finally {
  54. //close connection ,stmt and resultset here
  55. try {
  56. con.close();
  57. } catch (SQLException se) { /*can't do anything */ }
  58. try {
  59. stmt.close();
  60. } catch (SQLException se) { /*can't do anything */ }
  61. try {
  62. rs.close();
  63. } catch (SQLException se) { /*can't do anything */ }
  64. }
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement