jdbc使用druid的工具类小案例
实现查询
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//使用druid的工具类实现查询
public class JDBCdemo10 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//获取连接对象
connection = JDBCUntils.getConnection();
String sql = "select * from student";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println(name+": "+age);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JDBCUntils.close(connection,preparedStatement,resultSet);
}
}
}