jdbc模板占位符替换,处理结果集使用什么机制
1个回答
展开全部
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();
}
}
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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询