Advertisement
Guest User

Untitled

a guest
Nov 20th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. public class Console {
  2.  
  3. String sql;
  4. Statement stmt;
  5. Connection conn;
  6. ResultSet rs;
  7.  
  8. //Category Total
  9. public static double num;
  10.  
  11. public Console(){
  12. try{
  13. Class.forName("com.mysql.jdbc.Driver");
  14. System.out.println("Connecting to database...");
  15. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database?autoReconnect=true&useSSL=false","user","password");
  16. stmt = conn.createStatement();
  17. System.out.println("Connected database successfully...");
  18.  
  19. sql = "SELECT sum(a) FROM table";
  20. while(rs.next()) {
  21. rs = stmt.executeQuery(sql);
  22. num = rs.getDouble("sum(a)");
  23. }
  24. } catch(Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28.  
  29. public static void main(String args[]) {
  30. System.out.println(num);
  31. }
  32.  
  33. rs = stmt.executeQuery(...);
  34. while (rs.next()) {
  35. // and process your results row by row here...
  36. }
  37.  
  38. sql = "SELECT sum(a) FROM table";
  39. PreparedStatement preparedStatement = connection.prepareStatement(sql);
  40. ResultSet rs = preparedStatement.executeQuery();
  41. while (rs.next()) {
  42. num = rs.getDouble("sum(a)");
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement