Skip to content

Instantly share code, notes, and snippets.

@MrFant
Last active November 20, 2019 05:59
Show Gist options
  • Select an option

  • Save MrFant/20a15fc3f70236ed51fcba3fba037aee to your computer and use it in GitHub Desktop.

Select an option

Save MrFant/20a15fc3f70236ed51fcba3fba037aee to your computer and use it in GitHub Desktop.

Revisions

  1. MrFant revised this gist Nov 20, 2019. 1 changed file with 40 additions and 0 deletions.
    40 changes: 40 additions & 0 deletions jdbc2.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    import java.sql.*;

    public class jdbc{
    public static void main(String[] args) throws Exception {
    //1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动
    Class.forName("com.mysql.jdbc.Driver");//硬编码
    /*
    * 2、通过驱动管理者获取数据库连接
    * DriverManager是驱动实现类的管理者。
    * url:连接数据库的位置,端口号,数据库名
    * jdbc:mysql://localhost:3306/test_01
    */
    try(Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis", "root", "root")){
    /*
    * 3、获得sql语句执行者
    * statement:执行sql语句对象
    * sql语句必须是完整的。
    * preparedStatement:预处理(常用)
    * sql语句可以不是完整的,可以将参数用?替代,然后在预编译后加入未知参数
    *
    */
    //定义sql语句,?表示占位符
    String sql = "select * from student where id = ?"; //硬编码
    try(PreparedStatement ps = conn.prepareStatement(sql)){
    ps.setInt(1, 1); //硬编码
    //4、获取sql语句执行结果,resultset
    /*
    * executeQuery():查询操作
    * executeUpdate():增删改操作
    */
    try(ResultSet rs= ps.executeQuery()){
    //5、处理结果
    while(rs.next()){
    System.out.println(rs.getString(3));;//index代表第几列,从1开始
    }
    }
    }
    }
    }
    }
  2. MrFant revised this gist Nov 20, 2019. No changes.
  3. MrFant revised this gist Dec 5, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions jdbc.java
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,7 @@ public static void main(String[] args) throws Exception{
    //定义sql语句,?表示占位符
    String sql = "select * from user where id = ?"; //硬编码
    PreparedStatement ps = conn.prepareStatement(sql);
    // 设置第一个参数值为int类型的1,后面那个1是index,从1开始,不是从0开始。
    ps.setInt(1, 1); //硬编码
    //4、获取sql语句执行结果,resultset
    /*
  4. MrFant revised this gist Dec 5, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions jdbc.java
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // 依赖jdbc
    import java.sql.*;
    public class jdbc{
    public static void main(String[] args) throws Exception{
    //1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动
  5. MrFant revised this gist Dec 5, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jdbc.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    public class jdbc{
    public static void main(String[] args) throws exception{
    public static void main(String[] args) throws Exception{
    //1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动
    Class.forName("com.mysql.jdbc.Driver");//硬编码
    /*
  6. MrFant revised this gist Dec 4, 2018. No changes.
  7. MrFant revised this gist Dec 4, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jdbc.java
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    public class jdbc{
    public static void main(String[] args) throws exception{
    //1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动
    //1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动
    Class.forName("com.mysql.jdbc.Driver");//硬编码
    /*
    * 2、通过驱动管理者获取数据库连接
  8. MrFant revised this gist Dec 4, 2018. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions jdbc.java
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,6 @@
    public class jdbc{
    public static void main(String[] args) throws exception{

    }
    }
    //1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动
    //1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动
    Class.forName("com.mysql.jdbc.Driver");//硬编码
    /*
    * 2、通过驱动管理者获取数据库连接
    @@ -38,4 +35,6 @@ public static void main(String[] args) throws exception{
    //6、关闭连接
    rs.close();
    ps.close();
    conn.close();
    conn.close();
    }
    }
  9. MrFant revised this gist Dec 4, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion jdbc.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,7 @@
    public class jdbc{
    public static void main() throws exception
    public static void main(String[] args) throws exception{

    }
    }
    //1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动
    Class.forName("com.mysql.jdbc.Driver");//硬编码
  10. MrFant revised this gist Dec 4, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jdbc.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    public class jdbc{
    public static void main()
    public static void main() throws exception
    }
    //1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动
    Class.forName("com.mysql.jdbc.Driver");//硬编码
  11. MrFant revised this gist Dec 4, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion jdbc.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    public class jdbc
    public class jdbc{
    public static void main()
    }
    //1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动
    Class.forName("com.mysql.jdbc.Driver");//硬编码
    /*
  12. MrFant revised this gist Dec 4, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jdbc.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    public class j
    public class jdbc
    //1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动
    Class.forName("com.mysql.jdbc.Driver");//硬编码
    /*
  13. MrFant revised this gist Dec 4, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jdbc.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    public
    public class j
    //1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动
    Class.forName("com.mysql.jdbc.Driver");//硬编码
    /*
  14. MrFant revised this gist Dec 4, 2018. No changes.
  15. MrFant revised this gist Dec 4, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jdbc.java
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@

    public
    //1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动
    Class.forName("com.mysql.jdbc.Driver");//硬编码
    /*
  16. MrFant revised this gist Dec 4, 2018. 1 changed file with 37 additions and 1 deletion.
    38 changes: 37 additions & 1 deletion jdbc.java
    Original file line number Diff line number Diff line change
    @@ -1 +1,37 @@
    _

    //1、注册驱动,什么是驱动?能够让java连接数据库,也就是实现jdbc接口规范就是驱动
    Class.forName("com.mysql.jdbc.Driver");//硬编码
    /*
    * 2、通过驱动管理者获取数据库连接
    * DriverManager是驱动实现类的管理者。
    * url:连接数据库的位置,端口号,数据库名
    * jdbc:mysql://localhost:3306/test_01
    */
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_1", "root", "root");//(硬编码)
    /*
    * 3、获得sql语句执行者
    * statement:执行sql语句对象
    * sql语句必须是完整的。
    * preparedStatement:预处理(常用)
    * sql语句可以不是完整的,可以将参数用?替代,然后在预编译后加入未知参数
    *
    */
    //定义sql语句,?表示占位符
    String sql = "select * from user where id = ?"; //硬编码
    PreparedStatement ps = conn.prepareStatement(sql);
    ps.setInt(1, 1); //硬编码
    //4、获取sql语句执行结果,resultset
    /*
    * executeQuery():查询操作
    * executeUpdate():增删改操作
    */
    ResultSet rs= ps.executeQuery();
    //5、处理结果
    while(rs.next()){
    System.out.println(rs.getInt(1));;//index代表第几列,从1开始
    }

    //6、关闭连接
    rs.close();
    ps.close();
    conn.close();
  17. MrFant created this gist Dec 4, 2018.
    1 change: 1 addition & 0 deletions jdbc.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    _