一、数据库的基本操作
1.SQL语句四大类:
- (1)数据查询/操纵/定义/控制语言(DQL/DML/DDL/DCL)
- (2)增删改查CRUD
2.查询数据
- 利用Statement实例通过执行静态SELECT语句完成,也可以利用PreparedStatement实例通过执行SELECT语句进行完成,还可以利用CallableStatement实例通过执行存储过程来完成。
- (1)利用Statement实例通过执行静态SELECT语句查询的代码示例:
String sql = "select * from tb_record where sec=?";
Statement stmt = con.createStatement();
PreparedStatement pstmt = con.preparedStatement(sql);
CallableStatement cstmt = con.prepareCall("(CALL demoSp(?,?))");
- (2)利用PreaparedStatement实例通过执行动态SELECT语句查询数据的代码如下:
String sql = "select * from tb_record where sec=?";
PreparedStatement prpdStmt = connection.prepareStatement(sql);
prpdStmt.setString(1,"男");
ResultSet rs = prpdStmt.executeQuery();
- (3) 利用CallableStatement实例通过执行存储过程查询数据的代码示例如下:
String call = "(call pro_record_select_by_sex(?))";
CallableStatement cablStmt = connection.prepareCall(call);
calStmt.setString(1,"男");
ResultSet rs = cablStmt.executeQuery();
- 无论使用哪种方式,都需要执行executeQuery()方法,这时才是真正开始执行Select语句。
package com.bjpowernode.java_learning;
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class D135_1_SelectExercise {
private static final String URL = "jdbc:Access:///e:/xsgl.mdb";
static {
try {
Class.forName("com.hxtt.sql.access.AccessDriver");
}catch(ClassNotFoundException e) {
e.printStackTrace();//捕获未找到该类的异常信息
}
}
public static void main(String[] args) {
try {
Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement();
String sql = "select * from studentInfo"; //定义静态的SELECT语句
ResultSet rs = stmt.executeQuery(sql); //执行静态的SELECT语句
while(rs.next()) {
//遍历结果集,通过next()方法可以判断是否还存在符合条件的记录
int id = rs.getInt(1); //通过列索引获得指定列的值
String name = re.getString(2);
String sec = rs.getString(3);
System.out.println(id + " " + name + " " + sex);
}
stmt.close();
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
- 上面使用了Statement实例执行了静态SELECT语句,下面我们使用PreparedStatment实例执行动态SELECT语句查询记录
package com.bjpowernode.java_learning;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class D135_2_PreparedStatementSQL {
private static final String URL = "jdbc:Access:///e:/xsgl.mdb";
static {
try {
Class.forName("com.hxtt.sql.access.AccessDriver");
}catch(ClassNotFoundException e) {
e.printStackTrace(); //输出捕获到异常信息
}
}
public static void main(String[] args) {
try {
Connection conn = DriverManager.getConnection(URL);
Statement stmt = conn.createStatement(sql);
String sql = "select * from studentInfo"; //定义静态Select语句
PreparedStatement prpdStmt = conn.preparedStatement(sql); //预处理动态INSERT语句
ResultSet rs = prpdStmt.executeQuery(); //执行动态INSERT语句
ResultSetMetaData metaData = rs.getMetaData(); //获得ResultSetMetaData类的实例
System.out.println(metaData.getColumnName(1) + " ");//通过索引获得指定列的名称
System.out.println(metaData.getColumnName(2) + " ");
System.out.println(meatData.getColumnName(3) + " ");
while(rs.next()) {
int id = rs.getInt(1); //通过索引获得指定列的值
String name = rs.getString(2);
String sex = rs.getString(3);
System.out.println(id + " " + name + " " + sex);
}
rs.close();
prpdStmt.close();
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}
二、源码:
Java连载135-JDBC执行SQL操作