Java面试之熟练掌握JDBC

Java面试之熟练掌握JDBC

    • 传统的JDBC执行过程
    • 示例:

传统的JDBC执行过程

1.注册驱动

Class.forName("com.mysql.jdbc.Driver");

2.获取数据库连接

String strconnect="jdbc:mysql://localhost:3306/demo?user=root&password=root";
Connection connection=DriverManager.getConnection(strconnect);

3.创建一个Statement语句对象

Statement statement = connection.createStatement();

4.执行SQL语句

String sql="select * from demo1";
ResultSet resultset= statement.executeQuery(sql);

5.处理结果集

if (resultSet.next()){
int id=resultSet.getInt("id");
}

6.关闭资源

connection.close();
statement.close();
resultset.close();

示例:

public class JdbcDelete {

    //静态代码块
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //连接数据库
        String strconnect="jdbc:mysql://localhost:3306/demo?user=root&password=root";

        //空的连接
        Connection connection=null;
        //可以防止sql注入
        PreparedStatement preparedStatement=null;
        //处理结果
        ResultSet resultSet=null;


        try {
            //加载数据库
            connection=DriverManager.getConnection(strconnect);

            //创建sql语句
            String sql="select * from demo1";


            //创建statement对象,
            Statement statement = connection.createStatement();
            /*
            //执行sql语句
            int i = statement.executeUpdate(sql);

            if (i==1){
                System.out.println("输出成功");
            }else {
                System.out.println("输出失败");
            }
          */

            preparedStatement=connection.prepareStatement(sql);
            //preparedStatement.setInt(1, 1);
            //插入\删除\更新
            //int i=preparedStatement.executeUpdate();

            //查找
            resultSet = preparedStatement.executeQuery();

            //单行数据接收
            /*if (resultSet.next()){
                int id=resultSet.getInt("id");
                String name=resultSet.getString(2);
                String password=resultSet.getString("password");
                String status=resultSet.getString("status");

                System.out.println(id+name+password+status);
            }*/

            //多行数据接收
            while (resultSet.next()){
                int id=resultSet.getInt("id");
                String name=resultSet.getString(2);
                String password=resultSet.getString("password");
                String status=resultSet.getString("status");

                System.out.println(id+name+password+status);
            }


            //最重要的,connection,statement.resultset最后关闭
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //无论如何都执行关闭连接操作
              if (connection!=null){
                  try {
                      connection.close();
                  } catch (SQLException e) {
                      e.printStackTrace();
                  }
              }
        }
    }
}

你可能感兴趣的:(面试,jdbc)