Advertisement
Guest User

Untitled

a guest
Oct 4th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import java.sql.*;
  2. public class JDBCMain {
  3. public static void main(String[] args) {
  4. try {
  5. Class.forName("com.mysql.jdbc.Driver");
  6. } catch (ClassNotFoundException e) {
  7. System.err.println("JDBC-ajurin lataus epäonnistui");
  8. System.exit(0);
  9. }
  10. Connection connection = null;
  11. final String URL = "jdbc:mysql://localhost/autokanta";
  12. final String USER = "olso";
  13. final String PASSWD = "olso";
  14. Statement statement = null;
  15. ResultSet rs = null;
  16. try {
  17. connection = DriverManager.getConnection(URL, USER, PASSWD);
  18. statement = connection.createStatement();
  19. int x = statement.executeUpdate("INSERT INTO ajoneuvo (rekno,merkki)"
  20. + "VALUES ('AKU-313', 'Hyrysysy')");
  21. System.out.println("Pitäisi tulla 1: " + x);
  22. rs = statement.executeQuery("SELECT * FROM ajoneuvo");
  23. while (rs.next()) {
  24. String rsRekno = rs.getString("rekno");
  25. String rsMerkki = rs.getString("merkki");
  26. System.out.println(rsRekno + " " + rsMerkki);
  27. }
  28. } catch (SQLException e) {
  29. do {
  30. System.err.println("Viesti: " + e.getMessage());
  31. System.err.println("Virhekoodi: " + e.getErrorCode());
  32. System.err.println("SQL-tilakoodi: " + e.getSQLState());
  33. } while (e.getNextException() != null);
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. } finally {
  37. try {
  38. if (rs != null)
  39. rs.close();
  40. if (statement != null)
  41. statement.close();
  42. if (connection != null)
  43. connection.close();
  44. } catch (Exception e) {
  45. System.err.println("Resurssien vapautuksessa virhe");
  46. }
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement