关于java:jdbc访问数据库实现原理

42次阅读

共计 523 个字符,预计需要花费 2 分钟才能阅读完成。

1. 注册数据库驱动
Class.forName("com.mysql.jdbc.Driver");

2. 获取连贯
`Connection conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/jt_db?characterEncoding=utf-8”,

            "root", "root");`
           

3. 获取传输器
Statement stat = conn.createStatement();

4. 利用传输器发送 SQL 到服务器执行, 返回执行后果
`String sql = “select * from account”;

    ResultSet rs = stat.executeQuery(sql);`
    

5. 处理结果

6. 避免注入攻打办法
`ComboPooledDataSource pool = new ComboPooledDataSource();
Connection conn = pool.getConnection();
String sql = “select * from user”;
PreparedStatement ps =conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();`

正文完
 0