实例一:
目录结构:

mysql:

代码实现:
package demo1;
import java.sql.*;
public class Test3 {
public static void main(String[] args){
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.建立连接
//url的格式:主协议:子协议:名称
String url = "jdbc:mysql://127.0.0.1:3306/mysql_test?useUnicode=true&characterEncoding=utf8";
connection = DriverManager.getConnection(url,"root","13474501003");
//
String sql = "SELECT * FROM student";
statement = connection.createStatement();
//执行
//查询用excuteQuery方法
resultSet = statement.executeQuery(sql);
//处理结果
//resultSet.next():判断是否有下一行
while (resultSet.next()){
//rs.getXXXX():参数可以是列号(不推荐使用)、列名
int sno = resultSet.getInt("sno");
String sname = resultSet.getString("sname");
String sex = resultSet.getString("sex");
int age = resultSet.getInt("age");
String addr = resultSet.getString("addr");
System.out.println(sno+"\t"+sname+"\t"+sex+"\t"+age+"\t"+addr);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
//关闭
try {
if (resultSet != null){
resultSet.close();
}
if (statement != null){
statement.close();
}
if (connection != null){
connection.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
结果:

实例二:
目录结构:

mysql目录结构:

最初的student表:

代码实现:
Dbutil1:
package util;
import java.sql.*;
public class Dbuitl1 {
static Connection connection = null;
static PreparedStatement preparedStatement = null;
static ResultSet resultSet = null;
static{
try{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection(){
try {
//2.建立连接
//url的格式:主协议:子协议:名称
String url = "jdbc:mysql://127.0.0.1:3306/mysql_test?useUnicode=true&characterEncoding=utf8";
connection = DriverManager.getConnection(url,"root","13474501003");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return connection;
}
//创建PreparedStatement对象,并设置参数
public static void createPreparedStatement(String sql,Object...arr){
try {
preparedStatement = connection.prepareStatement(sql);
//设置参数
int i;
for (i = 0;i < arr.length;i++){
preparedStatement.setObject(i+1,arr[i]);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
//执行DML语句
public static int executeUpdate(){
int n = 0;
try {
n = preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return n;
}
//执行查询语句,返回ResultSet对象
public static ResultSet excuteQuery(){
try {
resultSet = preparedStatement.executeQuery();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return resultSet;
}
//关闭
public static void close(){
try {
if (resultSet != null){
resultSet.close();
}
if (preparedStatement != null){
preparedStatement.close();
}
if (connection != null){
connection.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
Test3:
package demo2;
import util.Dbuitl1;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test3 {
public static void main(String[] args){
Dbuitl1.getConnection();
String sql = "SELECT * FROM student WHERE age>?";
Dbuitl1.createPreparedStatement(sql,28);
ResultSet resultSet = Dbuitl1.excuteQuery();
try {
while (resultSet.next()){
int sno = resultSet.getInt("sno");
String name = resultSet.getString("sname");
System.out.println(sno+"\t"+name);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
Dbuitl1.close();
}
}
结果:
年龄大于28的

浙公网安备 33010602011771号