Guest User

Untitled

a guest
May 15th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. @Controller
  2. public class HomeController {
  3. public static void main(String args[]){
  4. }
  5. Hi hi=new Hi();
  6. @RequestMapping(value = "/")
  7. public String home(){
  8. return hi.getHello();
  9. }
  10. @RequestMapping(value="/form")
  11. public String form(){
  12. return "form";
  13. }
  14. @RequestMapping(value="/end") //method = RequestMethod.GET
  15. public String end(HttpServletRequest request, HttpServletResponse response) throws SQLException {
  16. String DB_URL = "jdbc:postgresql://127.0.0.1:5432/postgres";
  17. String USER = "postgres";
  18. String PASS = "1234";
  19. System.out.println("Testing connection to PostgreSQL JDBC");
  20. try {
  21. Class.forName("org.postgresql.Driver");
  22. } catch (ClassNotFoundException e) {
  23. System.out.println("PostgreSQL JDBC Driver is not found. Include it in your library path ");
  24. e.printStackTrace();
  25.  
  26. }
  27. System.out.println("PostgreSQL JDBC Driver successfully connected");
  28. Connection connection = null;
  29. try {
  30. connection = DriverManager
  31. .getConnection(DB_URL, USER, PASS);
  32.  
  33. } catch (SQLException e) {
  34. System.out.println("Connection Failed");
  35. e.printStackTrace();
  36.  
  37. }
  38.  
  39. if (connection != null) {
  40. System.out.println("You successfully connected to database now");
  41. } else {
  42. System.out.println("Failed to make connection to database");
  43. }
  44.  
  45. Statement statement=connection.createStatement();
  46. statement.executeUpdate("CREATE TABLE IF NOT EXISTS message (id SERIAL PRIMARY KEY , text CHAR(30) NOT NULL) ;");
  47. String s=null;
  48. request.getParameter(s);
  49. if (s!=null) {
  50. PreparedStatement preparedStatement=connection.prepareStatement("INSERT INTO message (text) VALUES (?);");
  51. preparedStatement.setString(1,s);
  52. }
  53. return "end";
  54. }
  55. }
Add Comment
Please, Sign In to add comment