完成数据库的连接,就马上要对数据库进行增删改查操作了;先来了解一下Statement

通过JDBC插入数据 (这里提供一个查找和插入方法)

Statement:用于执行sql语句的对象;
*1.通过Connection 的creatStatement()方法来获取;
*2.通过executeUpdate(sql) 可以执行SQL语句
*3.传入的SQL可以是insert update delete,但是不能是select;
* 注意:在使用后要关闭connection和statement(在finally中关闭)
* 关闭的顺序:先关statement ; 后关 connection

@Test
public void testStatement()throws Exception{
//1.获取数据库连接
Connection conn = null;
Statement st = null;
try {
conn = getConnection();//参看最底下的附录(获取连接)
//2.准备插入的sql
String sql ="insert into contacts(name,address,phone)"+"value('xyz','abc','abc')";
//3.执行插入
//获取sql的statement对象:Connection的createStatement()方法来获取
st = conn.createStatement();
//调用statement对象的executeUpdate(sql)对象,插入sql语句
st.executeUpdate(sql);
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(st !=null)
//关闭Statement 对象
st.close();
}catch (Exception e) {
e.printStackTrace();
}finally{
//关闭连接
if(conn !=null)
conn.close();
}

}

}

/**
* ResultSet:结果集,封装了使用JDBC进行查询的接口
* 1.调用Statement 对象的executeQuesry(sql)可以得到结果集
* 2.返回一张数据表,有指针指向数据表的第一行的前面
* 3.可以调用next方法检测下一行是否有效,若有效返回true且指针下移
* 4.当指针对到一行时,可以通过getxxxx(index)或者getxxx(cloumName);
* 获取每一列的值:getInt(1),getString("name")
* @throws Exception
*/
@Test
public void testResultSet() throws Exception{
//获取Id=19的记录值,并且每条打印
Connection con = null;
ResultSet rs = null;
Statement st = null;
try {
//1.获取connection
con = JdbcTools.getConnection();//JdbcTools.getConnection()同附录的方法
//2.获取Statement
st = con.createStatement();
//3.准备sql
String sql = "select * from contacts where id=19";
//4.获取ResultSet
rs = st.executeQuery(sql);
//5.处理ResultSet
if(rs.next()){//如果是多条记录,把if改成while
int id = rs.getInt(1);//第一列
String name = rs.getString("name");
String address = rs.getString(3);
System.out.println(id);
System.out.println(name);
System.out.println(address);
}
//6.关闭
} catch (Exception e) {
e.printStackTrace();
}finally{
JdbcTools.release(rs, st, con);//方法见附录
}
}

//以上提供了插入和查询方法

*********************************************************************************************************************

这里我们在来了解一个PreparedStatement方法,可以对比Stetement

/**
* PreparedStatement是Stetement的子接口;
*preparedStatement:使用statement,sql需要拼接;
*PerparedStatement:String sql="insert into contacts(name,address,phone) value(?,?,?)"
*Statement:String sql = "insert into contacts(name,address,phone)"+"value('nn'+'121'+'1212')";
*1.创建preparedStatement:
*2.调动PreparedStatement的setxxx(int index,object value),设置占位符的值
*这里index从1开始
*3.执行sql语句:executeQuery()或者executeUpdate(),前者是查询,后者是更新
*注意,执行时不再需要闯入sql语句
*
*好处:1防止SQl注入,即防止拼接过程中有加入其它东西,例如有OR···,这样,sql就可以正确执行;2提高性能;3····
*
* @throws Exception
*/
@Test
public void testPreparedStatement() throws Exception {
Connection con = null;
PreparedStatement p = null;
ResultSet rs = null;
try {
con = JdbcTools.getConnection();
String sql = " insert into contacts(name,address,phone)values(?,?,?)";
p = con.prepareStatement(sql);
p.setString(1, "tom");
p.setString(2, "zhejiang");
p.setString(3, "15988324290");
p.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
JdbcTools.releaseDB(rs, p, con);
}

}

附上

public Connection getConnection() throws Exception{
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;

//读取类路径下的properties
InputStream in = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties p = new Properties();
p.load(in);
driverClass=p.getProperty("driver");
jdbcUrl = p.getProperty("jdbcUrl");
user = p.getProperty("user");
password = p.getProperty("password");

Driver driver = (Driver) Class.forName(driverClass).newInstance();//实例化
Properties info = new Properties();
info.put("user",user);
info.put("password",password);
Connection connection = driver.connect(jdbcUrl, info);
return connection;
}

public static void release(ResultSet rs, Statement statement ,Connection con) throws Exception{

if(rs != null){

try {

rs.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

if(statement !=null){

try {

statement.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

if(con!=null){

try {

con.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

}

public static void releaseDB(ResultSet rs, PreparedStatement statement ,Connection con) throws Exception{

if(rs != null){

try {

rs.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

if(statement !=null){

try {

statement.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

if(con!=null){

try {

con.close();

} catch (Exception e2) {

e2.printStackTrace();

}

}

}

需要导入的包

mysql-connector-java-5.1.34.jar

jdbc 数据的增删改查的Statement Resultset PreparedStatement的更多相关文章

  1. MVC模式:实现数据库中数据的增删改查功能

    *.数据库连接池c3p0,连接mysql数据库: *.Jquery使用,删除时跳出框,确定是否要删除: *.使用EL和JSTL,简化在jsp页面中插入的java语言 1.连接数据库 (1)导入连接数据 ...

  2. Mybatis框架基于注解的方式,实对数据现增删改查

    编写Mybatis代码,与spring不一样,不需要导入插件,只需导入架包即可: 在lib下 导入mybatis架包:mybatis-3.1.1.jarmysql驱动架包:mysql-connecto ...

  3. dbutils中实现数据的增删改查的方法,反射常用的方法,绝对路径的写法(杂记)

    jsp的三个指令为:page,include,taglib... 建立一个jsp文件,建立起绝对路径,使用时,其他jsp文件导入即可 导入方法:<%@ include file="/c ...

  4. Mybatis实现数据的增删改查

    Mybatis实现数据的增删改查 1.项目结构(使用maven创建项目) 2.App.java package com.GetcharZp.MyBatisStudy; import java.io.I ...

  5. Hibernate3回顾-5-简单介绍Hibernate session对数据的增删改查

    5. Hibernate对数据的增删改查 5.1Hibernate加载数据 两种:get().load() 一. Session.get(Class arg0, Serializable arg1)方 ...

  6. Mybatis学习总结(二)—使用接口实现数据的增删改查

    在这一篇中,让我们使用接口来实现一个用户数据的增删改查. 完成后的项目结构如下图所示: 在这里,person代表了一个用户的实体类.在该类中,描述了相关的信息,包括id.name.age.id_num ...

  7. mvc模式jsp+servel+jdbc oracle基本增删改查demo

    mvc模式jsp+servel+jdbc oracle基本增删改查demo 下载地址

  8. 数据的增删改查(三层)<!--待补充-->

    进行数据操作必然少了对数据的增删改查,用代码生成器生成的代码不是那么满意!方便在今后使用,这里就主要写“数据访问层(Dal)” 既然这里提到三层架构:有必要将三层内容在这里详细介绍一下(待补充) 注: ...

  9. vue实现对表格数据的增删改查

    在管理员的一些后台页面里,个人中心里的数据列表里,都会有对这些数据进行增删改查的操作.比如在管理员后台的用户列表里,我们可以录入新用户的信息,也可以对既有的用户信息进行修改.在vue中,我们更应该专注 ...

随机推荐

  1. IntelliJ IDEA 14 注册码

    IntelliJ IDEA 14 下载地址: IntelliJ IDEA 14 下载 分享几个license: (1) key:IDEA value:61156-YRN2M-5MNCN-NZ8D2-7 ...

  2. 如何让win10实现关机确认-暂没确认

    为了实现关机时有提示确认,防止不小心触碰后不提示就关机了.本人安装有360软件小助手,发生过此事多次. 1.网上找到 http://zhidao.baidu.com/link?url=dYB0fl2S ...

  3. js 类型转换学习

    类型转换分为显示转换和隐式转换 参考http://www.cnblogs.com/mizzle/archive/2011/08/12/2135885.html 先事件显示的 通过手动进行类型转换,Ja ...

  4. ZeroMQ实例-使用ZMQ(ZeroMQ)进行局域网内网络通信

    本文内容摘要:1)安装zeromq.2)实例说明使用zmq进行网络间的消息发送和接收 首先在机器中安装zmq库 步骤如下: 1)下载zeromq的源代码,ZeroMQ的官方网址:http://zero ...

  5. java文件下载,上传,解压方法

    1.文件下载(亲测可用) private static final int BUFFER = 2 * 1024;// 缓冲区大小(2k)private boolean isSuccess = true ...

  6. 【转】安装第三方库出现 Python version 2.7 required, which was not found in the registry

    安装第三方库出现 Python version 2.7 required, which was not found in the registry 建立一个文件 register.py 内容如下. 然 ...

  7. matlab处理图像代码

    1.图像的读取MATLAB中从图像文件中读取数据用函数imread(),这个函数的作用就是将图像文件的数据读入矩阵中,此外还可以用imfinfo()函数查看图像文件的信息(见例1)%例1:图像数据及图 ...

  8. TotalCommander 之 配置

    一.设置配置界面: 1.进入设置界面       点击菜单栏的配置,然后再点击配置里面的选项,便会出现Total Commander设置的界面. 2.设置字体 刚开始,大家会发现这不是我们熟悉的字体啊 ...

  9. PHP中GBK和UTF8乱码解决方案

    我用的appserv-win32-2.5.10做的环境,装这个包的时候用默认的utf8编码.在写数据库连接文件时,写成: $conn = mysql_connect("$host" ...

  10. visual studio code 里调试运行 Python代码

    最近对微软的visual studio code 挺感兴趣的,微软的跨平台开发工具.轻量简洁. 版本迭代的也挺快的,截止16年8月2日已经1.3.1版本了,功能也愈加完善.(16年12月18日 已经, ...