jdbc模板(复制粘贴)
1、普通版
1.1、普通查询
public class MyTest {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
conn=
DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
//3.获取数据库操作对象
String sql = "select * from user_table";
ps = conn.prepareStatement(sql);
//4.执行sql
rs = ps.executeQuery();
//5.处理结果集
while(rs.next()){
String a = rs.getString(1);
String b = rs.getString(2);
String c = rs.getString(3);
System.out.println(a+","+b+","+c);
}
}catch(Exception e){
e.printStackTrace();
}finally {
//6.关闭资源
--------------------------------------------------------------------
try {
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (ps != null) {
ps.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (rs != null) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
--------------------------------------------
}
}
1.2、普通插入
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class Exer2 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
try{
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1","root","root");
//3.获取数据库操作对象
String sql = "insert into ccc values(9,'a',9)";
ps = conn.prepareStatement(sql);
//4.执行sql
int count = ps.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(conn != null){
conn.close();
}
}catch (Exception e){
e.printStackTrace();
}
try {
if(ps != null){
ps.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
2、安全插入
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class Exer3 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
try{
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1","root","root");
//3.获取数据库操作对象
String sql = "insert into ccc values(?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1,"8");
ps.setString(2,"v");
ps.setString(3,"8");
//4.执行sql
int count = ps.executeUpdate();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(conn != null){
conn.close();
}
}catch (Exception e){
e.printStackTrace();
}
try {
if(ps != null){
ps.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
3、将常用对象封装到DBUtil中
DBUtil类
public class DBUtil {
private DBUtil(){
}
static {
try {
Class.forName("con.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
}
public static void close(Connection connection, Statement ps,ResultSet rs){
try{
if(ps!=null){
ps.close();
}
}catch(Exception e){
e.printStackTrace();
}
try{
if(connection!=null){
connection.close();
}
}catch (Exception e){
e.printStackTrace();
}
try{
if(rs!=null){
rs.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
实现类
public class Thried {
public static void main(String[] args) {
Connection conn = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
//1.加载驱动
//2.获取连接
conn = DBUtil.getConnection();
//3.获取数据库操作对象
String sql = "select * from user_table";