Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1006717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:30:19+00:00 2026-05-16T08:30:19+00:00

I’m new to Java and even newer to java database connections. I’ve managed to

  • 0

I’m new to Java and even newer to java database connections. I’ve managed to create a database connection and query a table when I put it in the Main class. Now that I’ve moved it into a new class called Connection I am getting errors:

package lokate;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

public class Connection {

private static Statement stmt = null;
private static ResultSet rs = null;
private static Connection con = null;

public Connection() throws SQLException {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        String connectionUrl = "jdbc:mysql://localhost:3306/Lokate?" +
                               "user=root&password=";
        con = DriverManager.getConnection(connectionUrl);
        stmt = con.createStatement();
        retriveData("SELECT * FROM Users");
        int rowsEffected = 0;
    } catch (SQLException sqlEx) {
        System.out.println("SQL Exception: "+ sqlEx.toString());
    } catch (ClassNotFoundException classEx) {
        System.out.println("Class Not Found Exception: "+ classEx.toString());
    } catch (Exception Ex) {
        System.out.println("Exception: "+ Ex.toString());
    }
}

public static void retriveData(String SQL) throws Exception {
    rs = stmt.executeQuery(SQL);
    while (rs.next()) 
    {
        System.out.println(rs.getString("fname") + " : " + rs.getString("lname"));
    }
}

}

I’m getting an error saying cannot find symbol. Symbol:method createStatement() and incomparable types for con = DriveManager…..

Can anyone help?

Also, is it best practice to put the connection in the class like this then call a new object every time I want to do something with the db?

Regards,

Billy

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:30:20+00:00Added an answer on May 16, 2026 at 8:30 am

    I’d say your code is an example of many worst practices. Let me count the ways:

    1. Your Connection class is a poor abstraction that offers nothing over and above that of java.sql.Connection.
    2. If you use your class, you’ll never get to take advantage of connection pooling.
    3. You hard wire your driver class, your connection URL, etc. You can’t change it without editing and recompiling. A better solution would be to externalize such things.
    4. Printing an error message in the catch blocks is far less information than supplying the entire stack trace.
    5. Your code hurts my eyes. It doesn’t follow the Sun Java coding standards.
    6. Your retrieveData method is utterly worthless. What will you do with all those printed statements? Wouldn’t it be better to load them into a data structure or object so the rest of your code might use that information?
    7. It’s rowsAffected – “affect” is the verb, “effect” is the noun. Another variable that’s not doing any good.

    You’re on the wrong track. Rethink it.

    I think you’ll find this code more helpful.

    package persistence;
    
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class DatabaseUtils
    {
        public static Connection createConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException
        {
            Class.forName(driver);
    
            if ((username == null) || (password == null) || (username.trim().length() == 0) || (password.trim().length() == 0))
            {
                return DriverManager.getConnection(url);
            }
            else
            {
                return DriverManager.getConnection(url, username, password);
            }
        }
    
        public static void close(Connection connection)
        {
            try
            {
                if (connection != null)
                {
                    connection.close();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    
    
        public static void close(Statement st)
        {
            try
            {
                if (st != null)
                {
                    st.close();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    
        public static void close(ResultSet rs)
        {
            try
            {
                if (rs != null)
                {
                    rs.close();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    
        public static void rollback(Connection connection)
        {
            try
            {
                if (connection != null)
                {
                    connection.rollback();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    
        public static List<Map<String, Object>> map(ResultSet rs) throws SQLException
        {
            List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
    
            try
            {
                if (rs != null)
                {
                    ResultSetMetaData meta = rs.getMetaData();
                    int numColumns = meta.getColumnCount();
                    while (rs.next())
                    {
                        Map<String, Object> row = new HashMap<String, Object>();
                        for (int i = 1; i <= numColumns; ++i)
                        {
                            String name = meta.getColumnName(i);
                            Object value = rs.getObject(i);
                            row.put(name, value);
                        }
                        results.add(row);
                    }
                }
            }
            finally
            {
                close(rs);
            }
    
            return results;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.