Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package testingConnection;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.Statement;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.ResultSetMetaData;
  13. import java.sql.SQLException;
  14. /**
  15. *
  16. * @author namko
  17. */
  18. public class DisplayAuthors {
  19. static final String DATABASE_URL="jdbc:mysql://localhost/books";
  20. public static void main(String args[]){
  21. Connection connection = null;
  22. Statement statement = null;
  23. ResultSet resultSet = null;
  24. try
  25. {
  26. connection = DriverManager.getConnection(DATABASE_URL,"root","");
  27. statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
  28. resultSet = statement.executeQuery("SELECT * FROM authors");
  29.  
  30. while(resultSet.next()){
  31. resultSet.updateString("lastname","Karki");
  32. resultSet.updateRow();
  33. }
  34. resultSet.beforeFirst();
  35. ResultSetMetaData metaData = resultSet.getMetaData();
  36. int numberOfColumns = metaData.getColumnCount();
  37. System.out.println("Authors Table:");
  38.  
  39. for(int i=1; i<=numberOfColumns; i++)
  40. System.out.printf( "%-8s\t",metaData.getColumnName(i));
  41. System.out.println();
  42.  
  43. while(resultSet.next())
  44. {
  45. for ( int i=1; i<= numberOfColumns; i++ )
  46. System.out.printf( "%-8s\t",resultSet.getObject(i));
  47. System.out.println();
  48.  
  49. }
  50. }
  51. catch(SQLException sqlException)
  52. {
  53. sqlException.printStackTrace();
  54. }
  55. finally
  56. {
  57. try
  58. {
  59. resultSet.close();
  60. statement.close();
  61. connection.close();
  62. }
  63. catch (Exception exception)
  64. {
  65. exception.printStackTrace();
  66. }
  67. }
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement