Results 1 to 2 of 2

Thread: Jdbc

  1. #1
    NajwaMy is offline Member
    Join Date
    Jul 2013
    Posts
    13
    Rep Power
    0

    Default Jdbc

    Java Code:
    import java.sql.*;
    
    public class jdbcLab
    {
    	public static void main(String args[])
    	{
    		Connection conn = null;
    		Statement st = null;
    		ResultSet rs = null;
    		String url = "JDBC:mySQL://localhost/Employee";
    		String driver = "com.mysql.jdbc.Driver";
    		String user = "root";
    		String pass = "";
    
    		try
    		{
    			Class.forName(driver);
    			
    			//connect to database Employee
    			conn = DriverManager.getConnection(url,user,pass);
    
    			//create a table person
    			st = conn.createStatement();
    			st.executeUpdate("create table person(name varchar(20),  
                                     age int(20), address char(100))");
    
    			//insert new rows to table person
    			st.executeUpdate("insert  person (name,age,address) 
                                     values ('Rozana','34','Shah Alam, Selangor')");
    			
    
    			//query the table
    			rs = st.executeQuery("select * from person");
    
    			//browse the result
    			while (rs.next())
    			{
    				System.out.println("Name: "+rs.getString(1)+"\n");
    				System.out.println("Age: "+rs.getInt(2)+"\n");
    				System.out.println("Address: "+rs.getString(3)+"\n");
    			}
    
    			rs.close();
    			st.close();
    			
    			//disconnect from database
    			conn.close();
    		}
    
    		catch(SQLException e)
    		{	
    			System.out.println(e);
    		}
    
    		catch(ClassNotFoundException ee)
    		{
    			System.out.println(ee);
    		}
    	}
    }
    what does getString(1) represents in Line 38 .I get this codes from my lecturer.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: Jdbc

    What does your API documentation for the ResultSet interface say about it?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 06-22-2013, 05:26 PM
  2. Replies: 5
    Last Post: 09-25-2012, 05:36 PM
  3. JDBC Problem - com.mysql.jdbc.Driver
    By icu222much in forum Advanced Java
    Replies: 5
    Last Post: 11-22-2011, 04:54 PM
  4. Replies: 0
    Last Post: 04-01-2008, 10:17 AM
  5. Replies: 0
    Last Post: 09-28-2007, 12:56 PM

Tags for this Thread

Posting Permissions

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