当前位置:Gxlcms > 数据库问题 > sql-原生jdbc连接7步

sql-原生jdbc连接7步

时间:2021-07-01 10:21:17 帮助过:6人阅读

{ url = "jdbc:mysql://localhost:3306/test" ; driveClassName = "com.mysql.jdbc.Driver"; userName = "root" ; password = "root" ; }

2, 注册驱动

# 使用Class注册
 Class.forName(driveClassName);

# 使用DriverManager注册
DriverManager.registDriver( new com.mysql.jdbc.Driver() );

3, 获取链接

connect = DriverManager.getConnection(url, userName, password);

4, 执行sql语句

       String sql = "select * from sys_user";
            state = connect.prepareStatement(sql);

5, 获取结果集

 resultSet = state.executeQuery();

6, 结果集处理

      while (resultSet.next()) {
                String name = resultSet.getString("name");
                String sex = resultSet.getString(2);
                System.out.println(name + ": " + sex);
            }

7, 关闭连接

 if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (state != null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connect != null) {
                try {
                    connect.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

 

sql-原生jdbc连接7步

标签:ace   system   rac   select   get   链接   host   query   驱动   

人气教程排行