How to retrive a single data from a database



How to retrive a single data from a database..

Answer / Ankit Chaturvedi

To retrieve a single row (or record) of data from a database, you typically execute a SQL SELECT statement that includes the WHERE clause to filter the results. Here's an example using Java and JDBC:

```java
import java.sql.*;

public class SingleDataQuery {
public static void main(String[] args) throws Exception {
String url = "jdbc:mysql://localhost/myDatabase";
String user = "username";
String password = "password";
String query = "SELECT * FROM myTable WHERE id = 123"; // Replace 'id' and '123' with the appropriate column name and value.

Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);

if (rs.next()) {
System.out.println("ID: " + rs.getInt("id"));
System.out.println("Name: " + rs.getString("name"));
// Print other columns as needed
} else {
System.out.println("No data found.");
}
}
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Database Management Interview Questions

Define ddl and dml.

1 Answers  


What is the difference between a MESSAGEBOX and an ALERT ?

3 Answers  


What is the role of database server in database management system?

1 Answers  


Explain degree of a relation?

1 Answers  


What is indexing in database with example?

1 Answers  


What are different types of joins in the sql?

1 Answers  


what are the different kinds of indexing?

1 Answers  


How do I delete a database in phpmyadmin?

1 Answers  


Describe the differences between vertical and horizontal portioning.

1 Answers  


Can you explain insert, update and delete query?

1 Answers  


What are cursors give different types of cursors?

1 Answers  


What are the ways to tune reporting services?

1 Answers  


Categories