Results 1 to 2 of 2
  1. #1
    nick2price is offline Member
    Join Date
    May 2008
    Posts
    27
    Rep Power
    0

    Default JDBC between classes

    I have one class which has two methods. One which opens a connection and one that closes it.
    Java Code:
    import java.util.*;
    import java.sql.*;
    
    public class DatabaseUtils
    {    
        String driver = "oracle.jdbc.driver.OracleDriver";
        
        public static Connection con(String driver, String url, String username, String password) 
            throws ClassNotFoundException, SQLException
        {
            Class.forName(driver);
            
            return DriverManager.getConnection(url, username, password);
        }
     
        // Similar methods to close Statement and ResultSet
        public static void close(Connection connection) 
        {
            try
            {
                if (connection != null)
                {
                    connection.close();
                }
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    }
    Now in another class, i am trying to do a query. This is what i have done
    Java Code:
    import java.util.*;
    import java.sql.*;
    
    public class PersonDAO{      
    
    	public static void main(String args[])
    {
    String driver = "oracle.jdbc.driver.OracleDriver";	
    String url = "jdbc:oracle:thin:@localhost:1521:XE";
    String userName = "scott";
    String passWord = "tiger";
    PersonInfo person = new PersonInfo();
    DatabaseUtils.con("<driver>", "<url>", "<userName>", "<passWord>");
    
    		
    		try
    		{
    			
    			String sql = "INSERT INTO Person(name, idNo, " +
    							"userName, passWord) VALUES (?,?,?,?) ";
    
    			// Create a Preparedstatement
     			PreparedStatement ps = con.prepareStatement(sql);
    	
    			ps.setString(1, person.getName());
    			ps.setString(2, person.getIdNo());
    			ps.setString(3, person.getUsername());
    			ps.setString(4, person.getPassword());
    
    			ps.executeUpdate();
    		}
    		catch(Exception e){
    			System.out.println(e);
    		}
    	}
    	
    	}// end class PersonDAO
    I am being returned with the error cannot find symbal variable con, relating to the con.preparedstatement. Where abouts am i going wrong?

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    762
    Rep Power
    10

    Default

    You haven't declared any variable with the "con" name in your code. You should modify your way to obtain the connection object to:

    Java Code:
    Connection con = DatabaseUtils.con(driver, url, userName, passWord);
    Anyway, why did you cann the method with something like "<driver>", etc?

Similar Threads

  1. Cant run my classes
    By Assaf A in forum Eclipse
    Replies: 1
    Last Post: 04-22-2008, 02:31 PM
  2. Replies: 0
    Last Post: 04-01-2008, 10:17 AM
  3. Using a JAR from other classes
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-02-2008, 08:08 PM
  4. Replies: 0
    Last Post: 09-28-2007, 12:56 PM
  5. When do we use inner classes?
    By cruxblack in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 05:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •