Guest User

Untitled

a guest
Jul 30th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. Java database error
  2. import java.sql.* ;
  3.  
  4. class JDBCUpdate
  5. {
  6. public static void main( String args[] )
  7. {
  8. String DB_URL = "jdbc:mysql://localhost/mydatabase";
  9. try
  10. {
  11.  
  12. Class.forName( "com.mysql.jdbc.Driver" ) ;
  13.  
  14.  
  15.  
  16. // Database credentials
  17. String USER = "user";
  18. String PASS = "pass";
  19.  
  20. // Get a connection to the database
  21. Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
  22.  
  23.  
  24. // Print all warnings
  25. for( SQLWarning warn = conn.getWarnings(); warn != null; warn = warn.getNextWarning() )
  26. {
  27. System.out.println( "SQL Warning:" ) ;
  28. System.out.println( "State : " + warn.getSQLState() ) ;
  29. System.out.println( "Message: " + warn.getMessage() ) ;
  30. System.out.println( "Error : " + warn.getErrorCode() ) ;
  31. }
  32.  
  33. // Get a statement from the connection
  34. Statement stmt = conn.createStatement() ;
  35.  
  36. // Execute the Update
  37. int rows = stmt.executeUpdate( "UPDATE people SET id = 9842 WHERE birthyear=3434" ) ;
  38.  
  39. // Print how many rows were modified
  40. System.out.println( rows + " Rows modified" ) ;
  41.  
  42. // Close the statement and the connection
  43. stmt.close() ;
  44. conn.close() ;
  45. }
  46. catch( SQLException se )
  47. {
  48. System.out.println( "SQL Exception:" ) ;
  49.  
  50. // Loop through the SQL Exceptions
  51. while( se != null )
  52. {
  53. System.out.println( "State : " + se.getSQLState() ) ;
  54. System.out.println( "Message: " + se.getMessage() ) ;
  55. System.out.println( "Error : " + se.getErrorCode() ) ;
  56.  
  57. se = se.getNextException() ;
  58. }
  59. }
  60. catch( Exception e )
  61. {
  62. System.out.println( e ) ;
  63. }
  64. }
  65. }
  66.  
  67. UPDATE people SET id = 9842 WHERE birthyear=3434
Add Comment
Please, Sign In to add comment