To connect to a database in Java, you can use the JDBC (Java Database Connectivity) API. The JDBC API provides a standard way for Java programs to connect to and interact with databases.
Process the results of the query or update statement.
Close the connection object.
Here is an example of how to connect to a MySQL database in Java:
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) throws SQLException {
// Import the JDBC classes.
import java.sql.*;
// Create a connection object.
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "password");
// Create a statement object.
Statement statement = connection.createStatement();
// Execute a query.
String query = "SELECT * FROM users";
ResultSet resultSet = statement.executeQuery(query);
// Process the results of the query.
while (resultSet.next()) {
System.out.println(resultSet.getString("username"));
}
// Close the connection object.
connection.close();
}
}
In this example, we first import the JDBC classes. Then, we create a connection object using the
DriverManager.getConnection() method. Next, we create a statement object. Then, we execute a query using the
statement.executeQuery() method. Finally, we close the connection object.
Here is a more detailed explanation of each step:
Import the JDBC classes: You need to import the JDBC classes in order to use them in your Java program. The JDBC classes are located in the
java.sql package.
Create a connection object: The Connection object represents a connection to a database. You can create a connection object using the
DriverManager.getConnection() method. The DriverManager.getConnection() method takes three arguments: the database URL, the username, and the password.
Create a statement object: The Statement object represents a statement that can be executed against a database. You can create a statement object using the
connection.createStatement() method.
Execute a query or update statement: You can execute a query or update statement using the
statement.executeQuery() or statement.executeUpdate() methods. The
statement.executeQuery() method returns a ResultSet object that contains the results of the query. The
statement.executeUpdate() method does not return a ResultSet object.
Process the results of the query or update statement: If you execute a query, you can process the results of the query using the
ResultSet object. The ResultSet object provides methods for accessing the data in the results of the query.
Close the connection object: Once you are finished using the connection object, you should close it using the
connection.close() method. This will release the resources that are associated with the connection object.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
To connect to a database in Java, you can use the JDBC (Java Database Connectivity) API. The JDBC API provides a standard way for Java programs to connect to and interact with databases.
To connect to a database in Java, you need to:
Here is an example of how to connect to a MySQL database in Java:
import java.sql.*;
public class JDBCExample {
}
In this example, we first import the JDBC classes. Then, we create a connection object using the
DriverManager.getConnection()method. Next, we create a statement object. Then, we execute a query using thestatement.executeQuery()method. Finally, we close the connection object.Here is a more detailed explanation of each step:
java.sqlpackage.Connectionobject represents a connection to a database. You can create a connection object using theDriverManager.getConnection()method. TheDriverManager.getConnection()method takes three arguments: the database URL, the username, and the password.Statementobject represents a statement that can be executed against a database. You can create a statement object using theconnection.createStatement()method.statement.executeQuery()orstatement.executeUpdate()methods. Thestatement.executeQuery()method returns aResultSetobject that contains the results of the query. Thestatement.executeUpdate()method does not return aResultSetobject.ResultSetobject. TheResultSetobject provides methods for accessing the data in the results of the query.connection.close()method. This will release the resources that are associated with the connection object.