0

So Sorry if this Looks newbie Question, Im not Skilled in Mysql, I need to Get Entries of All Rows in My Mysql Database like We Do with Sqlite in Android :

 Cursor mCursor = mDb.query("MyTable",new String[]{"My Column"},null,null.null,null);
 ArrayList<> mylist = new ArrayList<>();

 do{

 mylist.add(mCursor.getString(mCursor.getColumnIndex("_id")));

 while(mCursor.moveToNext());

How does This Work in Mysql Guys?

5
  • what programming language ? Commented Aug 30, 2014 at 10:24
  • try this article : vogella.com/tutorials/MySQLJava/article.html Commented Aug 30, 2014 at 10:27
  • If you need the SQL query for fetching all the rows from a database table, then the query you need to write is: SELECT * FROM db_name.table_name Commented Aug 30, 2014 at 10:54
  • Do you also want to know how to establish database connection and how to execute a MYSQL queryin java? Commented Aug 30, 2014 at 10:56
  • docs.oracle.com/javase/tutorial/jdbc/index.html Commented Aug 30, 2014 at 11:04

1 Answer 1

3

first you have to create connection to your database, You can use JDBC for it.

Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;

public Dbconnection()
 {
    try{

            Class.forName("com.mysql.jdbc.Driver");
                 con=DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","root","root"); 
        }
        catch(Exception e)
        {
            System.out.println("Error in connection"+e);
        }
}

connection created in the constructor,

method to get all rows from a table

public void getData()
{
  try{
            ps=con.prepareStatement("select * from tbl_name");

            rs=ps.executeQuery();
            while(rs.next())
            {
                System.out.println(rs.getString(1)); //here you can get data, the '1' indicates column number based on your query

            }

      }
      catch(Exception e)
      {
          System.out.println("Error in getData"+e);
      }

}

In the while loop you can all data from rs using rs.getString(1) NB: change column number for different column.

To get more idea please refer the links:

http://www.tutorialspoint.com/jdbc/index.htm
http://www.w3schools.com/sql/ 
Sign up to request clarification or add additional context in comments.

1 Comment

not toally true, but like 90% of it was, and dealed with it thanks alot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.