Advertisement
Guest User

Untitled

a guest
Oct 29th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 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 javaapplication8;
  7.  
  8. /**
  9. *
  10. * @author Student
  11. */import java.sql.*;
  12. public class Konekcija {
  13. public static void main(String[] args)
  14. {
  15. try
  16. {
  17.  
  18. String dbUrl="jdbc:mysql://localhost:3309/itsbaza";
  19. String user= "root";
  20. String pass= "";
  21.  
  22. Class.forName("com.mysql.jdbc.Driver");
  23. Connection veza= DriverManager.getConnection(dbUrl,user,pass);
  24. Statement st=veza.createStatement();
  25. String upit="select * from studentnovi";
  26. try {
  27. ResultSet rezultat=st.executeQuery(upit);
  28. System.out.println("Ispravljacki program (drajver) je ucitan!");
  29. while(rezultat.next())
  30. {
  31. System.out.println(rezultat.getString("ime")+ " " +
  32. rezultat.getString("prezime") + " " +rezultat.getString("BrojIndeksa")
  33. + " " +rezultat.getString("smer"));
  34. };
  35. String upit2="INSERT into studentnovi(BrojIndeksa,ime,prezime,smer) VALUES (?,?,?,?)";
  36. PreparedStatement pstate= veza.prepareStatement(upit2);
  37. pstate.setString(1, new String("123"));
  38. pstate.setString(2, new String("Pera"));
  39. pstate.setString(3, new String("PEric"));
  40. pstate.setString(4, new String("Mata"));
  41. try {
  42. pstate.executeUpdate();
  43. } catch (SQLException e) {
  44. System.out.println("Izuzetak. " + e);
  45. }
  46.  
  47. } catch (Exception e) {
  48. System.out.println("Pogresan upit");
  49. }
  50.  
  51.  
  52. }
  53. catch ( SQLException ex)
  54. {
  55. System.out.println("Izuzetak. " + ex);
  56. }
  57. catch ( ClassNotFoundException e)
  58. {
  59. System.out.println("Upravljacki program nije caitan." + e);
  60. }
  61.  
  62.  
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement