package JDBC;
import java.sql.*;
public class JDBCDemo {
public static void main(String[] args) throws Exception {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://127.0.0.1:3306/bookmanagement";
String username = "root";String password = "root";Connection conn = DriverManager.getConnection(url,username,password);//3.定义sql语句String sql = "select * from allbook where id = 20";//4.获取执行sql的对象StatementStatement stmt = conn.createStatement();//5.执行sql,ResultSet对象包含了SQL查询的结果集。它类似于一个表,有行和列。每一行代表查询结果中的一个记录,每一列代表记录中的一个字段。你可以通过ResultSet对象提供的各种方法(如next(), getString(String columnName), getInt(int columnIndex)等)来遍历结果集并获取数据。ResultSet rst = stmt.executeQuery(sql);//6.处理结果while(rst.next()){System.out.println(rst.getString("id")+" "+rst.getString("username")+rst.getString("sign")+rst.getString("sex")+rst.getString("city")+rst.getString("experience")+rst.getString("place"));}//7.释放资源rst.close();stmt.close();conn.close();
}
}