jdbc
总结:
Statement(PreparedStatement):增删改查(通过Connection产生)
CallableStatement:调用数据库中的存储过程/存储函数(通过Connection产生)
Result:返回的结果集(上面的Statement等产生)
Connection产生操作数据库的对象:
Statement操作数据库:
增删改:executeUpdate()
查询:executeQuery;
ResultSet:保存结果集 select * from xxx
next() : 光标下移,判断是否有下一条数据:true/false
previous():true/false (往上)
getXxx(字段名 or 位置):获取具体的字段值
RreparedStatement操作数据库:
public interface PreparedStatement extends Statement
因此
增删改:executUpdate()
查询:executeQuery()
赋值操作: setXxx();
3. jdbc访问数据库的具体步骤:
①导入驱动,加载具体的驱动类
②与数据库建立链接
③发送sql,执行
package JdebcDemo01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.cj.Query;
public class JDBCDemo
{
private static final String URL="jdbc:mysql://localhost:3306/adds";
private static final String username="root";
private static final String password="123456";
static Connection connection=null;
static Statement createStatement=null;
static ResultSet resultSet;
public static void update() {//增删改
try {
//①导入
Class.forName("com.mysql.jdbc.Driver");
//②与数据库建立连接,首先通过DriverManager建立连接
connection = DriverManager.getConnection(URL, username, password);//ctrl+1返回值
//③发送sql, 执行(①增删改,②查(两大类))
createStatement = connection.createStatement();
//String sql="insert into student values(1,'zs',33,'mz')";//增(插入)
// String sql="update student set name='ls' where id=1";//修改
String sql="delete from student where id=1"; //删除
//执行SQL(增删改executeUpdate(),查询executeQuery())
int count=createStatement.executeUpdate(sql);
//处理结果
if (count>0) {
System.out.println("操作成功");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.