jdbc连接数据库代码
Sure, here's a simple example of Java code using JDBC to connect to a MySQL database. Make sure you have the MySQL JDBC driver (JAR file) in your classpath.
javaimport java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseConnectionExample {
public static void main(String[] args) {
// JDBC URL, username, and password of MySQL server
String url = "jdbc:mysql://localhost:3306/your_database_name";
String user = "your_username";
String password = "your_password";
// Establishing the connection
try (Connection connection = DriverManager.getConnection(url, user, password)) {
System.out.println("Connected to the database!");
// Perform database operations here
} catch (SQLException e) {
System.err.println("Connection failed. Error: " + e.getMessage());
}
}
}
Remember to replace "your_database_name," "your_username," and "your_password" with your actual database name, username, and password.
Make sure you have the MySQL JDBC driver in your classpath. You can download it from the official MySQL website: https://dev.mysql.com/downloads/connector/j/
If you're using a different database, you'll need to change the JDBC URL accordingly. Additionally, make sure to handle exceptions appropriately in a production environment.
Certainly! Here's an extension to the previous example, including a simple query execution:
javaimport java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DatabaseConnectionExample {
public static void main(String[] args) {
// JDBC URL, username, and password of MySQL server
String url = "jdbc:mysql://localhost:3306/your_database_name";
String user = "your_username";
String password = "your_password";
// Establishing the connection
try (Connection connection = DriverManager.getConnection(url, user, password)) {
System.out.println("Connected to the database!");
// Performing a simple query
String query = "SELECT * FROM your_table_name";
try (PreparedStatement preparedStatement = connection.prepareStatement(query);
ResultSet resultSet = preparedStatement.executeQuery()) {
// Processing the result set
while (resultSet.next()) {
// Retrieve data from the result set
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
// Print the data
System.out.println("ID: " + id + ", Name: " + name);
}
} catch (SQLException e) {
System.err.println("Error executing query. Error: " + e.getMessage());
}
} catch (SQLException e) {
System.err.println("Connection failed. Error: " + e.getMessage());
}
}
}
In this example, I added a simple query to select all records from a table. Make sure to replace "your_table_name" with the actual name of your table.
Remember to handle exceptions appropriately in a production environment and close resources properly. Additionally, you might need to adjust the SQL query and result processing based on your specific database schema and requirements.