-
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
-
*/
-
publicclass TestJdbcNew {
-
/**
-
* 上一篇文章,我们对JDBC有了初步的了解,并且知道了如何使用JDBC了。
-
* 但是,那只是JDBC的了解性操作实例。实际开发中,我们是不能那么玩的!!
-
*
-
* 下面我们学习一下:JDBC的占位符的使用,以后写程序建议都要这么玩的!!
-
*
-
* 注意事项:我们的异常应该捕获;而不是抛出来啊!!
-
*
-
* SQLException是非运行时异常,必须要捕获或者向上抛出!!
-
*/
-
publicstaticvoid 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 地址
-
*/
-
publicstaticvoid 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
-
*/
-
publicstaticvoid 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
-
*/
-
publicstaticvoid 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
-
*/
-
publicstaticvoid 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();
-
}
-
}
下一篇文章,我们将学习javaBean+DAO,这是开发的基础。上面两篇学习总结是基础的基础。要理解,熟练掌握!
看下下面这篇文章也不错!
JDBC为什么要使用PreparedStatement而不是Statement
本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/925308,如需转载请自行联系原作者