Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4.  
  5. public class Main{
  6.  
  7. static final String DB_URL = "jdbc:mysql://ricky.heliohost.org/cackoa_database";
  8.  
  9. static final String USER = "cackoa_user";
  10. static final String PASS = "cackoa_password";
  11.  
  12. public static void main(String args[]){
  13.  
  14.  
  15. Connection polaczenie = null;
  16. java.sql.Statement stmt = null;
  17. try{
  18. // Połączenie z bazą danych
  19. System.out.println("Connecting to database...");
  20. polaczenie = DriverManager.getConnection(DB_URL,USER,PASS);
  21. // Stworzenie zapytania oraz jego wykonanie
  22. System.out.println("Creating statement...");
  23. stmt = polaczenie.createStatement();
  24. String sql2;
  25. sql2= "INSERT INTO student (ID,name,surname,student_group) VALUES (69,'Sebastian','Chojnacki',4)";
  26.  
  27. stmt.execute(sql2);
  28.  
  29. // Czyszczenie po sobie
  30. stmt.close();
  31. polaczenie.close();
  32. }
  33.  
  34. catch(SQLException se){
  35. //Errory JDBC
  36. se.printStackTrace();
  37. }
  38. catch(Exception e){
  39. e.printStackTrace();
  40. }
  41. finally{
  42. //finally block - by pozamykać resources
  43. try{
  44. if(stmt!=null)
  45. stmt.close();
  46. }
  47. catch(SQLException se2){
  48. }// nothing we can do
  49. try{
  50. if(polaczenie!=null)
  51. polaczenie.close();
  52. }
  53. catch(SQLException se){
  54. se.printStackTrace();
  55. }//end finally try
  56. }//end try
  57. System.out.println("Koniec!");
  58.  
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement