Advertisement
Guest User

Untitled

a guest
Mar 12th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.ResultSetMetaData;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.ArrayList;
  8.  
  9. public class Conn {
  10.  
  11. public static void main(String[] args) {
  12. Connection connection2 = null;
  13. Statement statement2 = null;
  14. ResultSet resultSet2 = null;
  15.  
  16. try {
  17. connection2 = (Connection) DriverManager.getConnection(
  18. "jdbc:mysql://localhost/ИМЯ", "ЛОГИН", "ПАРОЛЬ");
  19. } catch (SQLException se) {
  20. }
  21.  
  22. try {
  23. statement2 = connection2.createStatement();
  24. } catch (SQLException se) {
  25. se.printStackTrace();
  26. }
  27.  
  28. try {
  29. resultSet2 = statement2.executeQuery("SELECT * FROM userdetails");
  30. } catch (SQLException se) {
  31. se.printStackTrace();
  32. }
  33.  
  34. try {
  35. ResultSetMetaData rsmd = resultSet2.getMetaData();
  36. int columns = rsmd.getColumnCount();
  37. for (int i = 1; i <= columns; i++)
  38. System.out.printf("%-8st", rsmd.getColumnName(i) + " ");
  39. System.out.println();
  40. for (; resultSet2.next();) {
  41. for (int j = 1; j <= columns; j++)
  42. System.out
  43. .printf("%-8st", resultSet2.getObject(j) + " ");
  44. System.out.println();
  45. }
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. } finally {
  49. try {
  50. connection2.close();
  51. statement2.close();
  52. resultSet2.close();
  53. } catch (SQLException se) {
  54. se.printStackTrace();
  55. }
  56. }
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement