您的位置:首页 > 数据库

JDBC(二)Statement,PrepareStatement和ResultSet

2016-07-26 05:02 483 查看

Statement,PrepareStatement和ResultSet

Statement

获取了数据库的连接后就可以使用statement对象来对数据库进行相应的操作(执行sql语句)。

数据库有一张数据表



通过statement对象来执行sql语句,向数据库中插入记录。

package com.aaa.package1;

import java.sql.Connection;
import java.sql.Statement;

public class TestStatement {

public static void main(String[] args) throws Exception {
// 1.获取数据库连接
Connection connection = JDBCTools.getConnection();
// 准备sql语句
String sql = " INSERT INTO users(user_name,user_password) VALUES ('Kate','222333')";
// 获取statement对象
Statement statement = connection.createStatement();
// 执行sql语句
statement.executeUpdate(sql);
// 关闭资源
JDBCTools.release(statement, connection, null);
}
}


执行完成后:



PrepaStatement

在这里可以使用PrepareStatement来执行,主要优点:

在利用statement时候需要写入完整的sql语句,当sql语句带有参数时候,只能通过字符串拼接的方式来完成,而是用prepareStatement对象来执行 的时候可以通过占位符来然后在使用对应的set方法来赋值,更为简单。

PreparedStatement在connection.prepareStatement(sql)传入sql语句时数据库服务器端会对其进行预编译,在下次遇到相同的sql语句是就可以直接执行。可提高效率,在某些情况下。

preparestatement可以防止sql注入。

使用prepareStatement对象来实现数据库操作:

package com.aaa.package1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TestPrepareStatement {

public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
// 获取数据连接
connection = JDBCTools.getConnection();
// 准备sql语句
String sql = "INSERT INTO users (user_name,user_password) VALUES (?,?)";
// 获取prepareStatement对象,传入sql
preparedStatement = connection.prepareStatement(sql);
// 赋值
preparedStatement.setString(1, "Tom");
preparedStatement.setString(2, "333444");
// 执行更新
preparedStatement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 释放资源
JDBCTools.release(preparedStatement, connection, null);
}
}
}


ResultSet

当从数据库查询时,会返回数据给应用程序,resultSet就是封装了查询结果的结果集

ResultSet 返回的实际上就是一张数据表. 有一个指针指向数据表的第一样的前面. 可以调用 next() 方法检测下一行是否有效,若有效该方法返回 true, 且指针下移. 相当于 Iterator 对象的 hasNext() 和 next() 方法的结合体

当指针对位到一行时, 可以通过调用 getXxx(index) 或 getXxx(columnName) 获取每一列的值. 例如: getInt(1), getString(“name”).

获取所有的user记录,并打印在控制台:

package com.aaa.package1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestResultSet {

public static void main(String[] args) {

Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
// 获取数据连接
connection = JDBCTools.getConnection();
// 准备sql语句
String sql = "SELECT * FROM users";
// 获取prepareStatement对象,传入sql
preparedStatement = connection.prepareStatement(sql);
// 执行查询
resultSet = preparedStatement.executeQuery();
// 处理结果集
while (resultSet.next()) {
int userID = resultSet.getInt(1);// 从1开始
String userName = resultSet.getString(2);
String userPassword = resultSet.getString(3);
System.out.print(userID + " " + userName + "    " + userPassword);
System.out.println();

}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 释放资源
JDBCTools.release(preparedStatement, connection, null);
}
}

}


数据库:



控制台:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jdbc 数据库 数据