Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Console {
- String sql;
- Statement stmt;
- Connection conn;
- ResultSet rs;
- //Category Total
- public static double num;
- public Console(){
- try{
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Connecting to database...");
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database?autoReconnect=true&useSSL=false","user","password");
- stmt = conn.createStatement();
- System.out.println("Connected database successfully...");
- sql = "SELECT sum(a) FROM table";
- while(rs.next()) {
- rs = stmt.executeQuery(sql);
- num = rs.getDouble("sum(a)");
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String args[]) {
- System.out.println(num);
- }
- rs = stmt.executeQuery(...);
- while (rs.next()) {
- // and process your results row by row here...
- }
- sql = "SELECT sum(a) FROM table";
- PreparedStatement preparedStatement = connection.prepareStatement(sql);
- ResultSet rs = preparedStatement.executeQuery();
- while (rs.next()) {
- num = rs.getDouble("sum(a)");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement