jdbc模板占位符替换,处理结果集使用什么机制

 我来答
xiangjuan314
2016-05-03 · TA获得超过3.3万个赞
知道大有可为答主
回答量:2.9万
采纳率:0%
帮助的人:2732万
展开全部
package com.hanchao.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
* jdbc学习总结二
* @author hanlw
* 2012-07-09
*/
public class TestJdbcNew {

/**
* 上一篇文章,我们对JDBC有了初步的了解,并且知道了如何使用JDBC了。
* 但是,那只是JDBC的了解性操作实例。实际开发中,我们是不能那么玩的!!
*
* ★下面我们学习一下:JDBC的占位符的使用,以后写程序建议都要这么玩的!!
*
* 注意事项:我们的异常应该捕获;而不是抛出来啊!!
*
* SQLException是非运行时异常,必须要捕获或者向上抛出!!★
*/

public static void main(String[] args) throws Exception {
/**
* 1.jdbc对Mysql的insert操作
*/
// insert("cherry","shanghai");

/**
* 2.jdbc对mysql的update操作
*/
// update("update",12);

/**
* 3.jdbc对mysql的delete操作
*/
// delete(12);

/**
* 4.jdbc对mysql的retrieve 操作
*/
retrieve(15);
}

/**
* jdbc对mysql的insert操作
*
* @param username 用户名
* @param address 地址
*/
public static void insert(String username,String address) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");

//注意下面几行★
String sql = "insert into t_user(username,address) values(?,?)"; //★
PreparedStatement sta = con.prepareStatement(sql);
sta.setString(1, username);
sta.setString(2, address);

int rows = sta.executeUpdate();
if(rows > 0) {
System.out.println("operate successfully!");
}
sta.close();
con.close();
}

/**
* jdbc对mysql的update操作
*
* @param address 地址
* @param id 主键值
* @throws Exception
*/
public static void update(String address,int id) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");

//★注意下面几行代码
String sql = "update t_user set address=? where id=?";
PreparedStatement sta = con.prepareStatement(sql);
sta.setString(1, address);
sta.setInt(2, id);

int rows = sta.executeUpdate();
if(rows > 0) {
System.out.println("operate successfully");
}
sta.close();
con.close();

}

/**
* jdbc对mysql的删除操作
*
* @param id
* @throws Exception
*/
public static void delete(int id) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");

//★注意点
String sql = "delete from t_user where id=?";
PreparedStatement sta = con.prepareStatement(sql);
sta.setInt(1, id);

int rows = sta.executeUpdate();
if(rows > 0) {
System.out.println("operate successfully!!");
}
sta.close();
con.close();
}

/**
* jdbc对mysql的retrieve操作
*
* @param id
* @throws Exception
*/
public static void retrieve(int id) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");

//注意★
String sql = "select id,username,address from t_user where id=?";
PreparedStatement sta = con.prepareStatement(sql);
sta.setInt(1, id);

ResultSet rs = sta.executeQuery();
//注意:当现实一条记录时:while可以换成if。★
if(rs.next()) {
int did = rs.getInt("id");
String username = rs.getString("username");
String address = rs.getString("address");
System.out.println(did + "\t" + username + "\t" + address);
}

rs.close();
sta.close();
con.close();
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式