Guest User

Untitled

a guest
May 25th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. CODE:
  2.  
  3. /*
  4. * To change this license header, choose License Headers in Project Properties.
  5. * To change this template file, choose Tools | Templates
  6. * and open the template in the editor.
  7. */
  8. package aaa;
  9. import static aaa.DB.geom;
  10. import static aaa.DB.getConnection;
  11. import java.sql.DriverManager;
  12. import java.sql.Connection;
  13. import java.sql.PreparedStatement;
  14. import java.sql.ResultSet;
  15. import java.sql.SQLException;
  16. import java.sql.Statement;
  17.  
  18. public class JDBCExample {
  19.  
  20. public static void main(String[] argv) throws SQLException {
  21.  
  22.  
  23.  
  24. try {
  25.  
  26. Class.forName("org.postgresql.Driver");
  27. // Class.forName("com.mysql.jdbc.Driver");
  28.  
  29. } catch (ClassNotFoundException e) {
  30.  
  31. System.out.println("Where is your PostgreSQL JDBC Driver? " +
  32. "Include in your library path!");
  33. e.printStackTrace();
  34. return;
  35.  
  36. }
  37.  
  38. System.out.println("PostgreSQL JDBC Driver Registered!");
  39.  
  40. Connection connection = null;
  41.  
  42. try {
  43.  
  44. connection = DriverManager.getConnection(
  45. "jdbc:postgresql://localhost:5432/postgres", "postgres",
  46. "abc");
  47.  
  48. } catch (SQLException e) {
  49.  
  50. System.out.println("Connection Failed! Check output console");
  51. e.printStackTrace();
  52. return;
  53.  
  54. }
  55.  
  56. if (connection != null) {
  57. System.out.println("You made it, take control your database now!");
  58.  
  59. //Connection conn = getConnection();
  60.  
  61.  
  62. connection.setAutoCommit(false);
  63. Statement s = null;
  64. try {
  65. s = connection.createStatement();
  66. } catch (Exception e) {
  67. System.out.println("statmnt connection not works");
  68. }
  69. PreparedStatement ss = connection.prepareStatement("SELECT * FROM nodes_road_geoms");
  70. try {
  71. ss.executeUpdate();
  72. } catch (Exception e) {
  73. System.out.println("statmnt excute update connection not works: ");
  74. e.printStackTrace();
  75. }
  76. String query = "CREATE TABLE COMPANY(ID INT );";
  77.  
  78. ResultSet r = s.executeQuery(query);
  79. connection.commit();
  80. } else {
  81. System.out.println("Failed to make connection!");
  82. }
  83. }
  84.  
  85. }
  86.  
  87. RUN:
  88.  
  89. -------- PostgreSQL JDBC Connection Testing ------------
  90.  
  91. PostgreSQL JDBC Driver Registered!
  92.  
  93. You made it, take control your database now!
  94.  
  95. Exception in thread "main" java.lang.NoSuchMethodError:
  96.  
  97. org.postgresql.core.BaseConnection.getPreferQueryMode()Lorg/postgresql/jdbc/PreferQueryMode;
  98. at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:151)
  99. at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:132)
  100. at aaa.JDBCExample.main(JDBCExample.java:69)
  101. C:UsersDellAppDataLocalNetBeansCache8.2executor-snippetsrun.xml:53: Java returned: 1
  102. BUILD FAILED (total time: 0 seconds)
  103.  
  104.  
  105.  
  106. QUESTION:Give me steps to solve it since database is connected already! What is the core of the problem?
  107. The problem is that if database postgresql connected then why not insert into database. The tables are also available and seen from netbeans.
Add Comment
Please, Sign In to add comment