| import java.sql.*; |
| |
| public class JDBCDemo { |
| public static void main(String[] args) { |
| String url = "jdbc:mysql://localhost:3306/mydatabase"; |
| String user = "username"; |
| String password = "password"; |
| |
| try (Connection conn = DriverManager.getConnection(url, user, password); |
| Statement stmt = conn.createStatement(); |
| ResultSet rs = stmt.executeQuery("SELECT * FROM mytable")) { |
| |
| while (rs.next()) { |
| // 处理查询效果 |
| System.out.println(rs.getString("column1") + ", " + rs.getInt("column2")); |
| } |
| |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
注意:上述代码仅作为示例,现实利用时需要根据具体数据库和表布局举行修改。同时,为了确保代码的结实性,需要添加适当的非常处理机制。