jdbc 预编译 mysql,​JDBC 预编译 增删改查

  • Post author:
  • Post category:mysql


​创建 book 表

create table book

(

id int primary key auto_increment,

title varchar(100),

price decimal(10,2),

birth TIMESTAMP,

publish_date TIMESTAMP,

update_date TIMESTAMP

);

2.JDBC 预编译方式 处理速度比较快, 它会将 sql 预编译缓存

预编译方式  添加 book 记录

package com.jdbc4;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import java.sql.Timestamp;

import java.util.Date;

public class TestAdd {

/**

* @param args

*/

public static void main(String[] args) {

Connection conn=null;

try {

Class.forName(“com.mysql.jdbc.Driver”);

//建立连接  ctrl+shfit +o

conn=DriverManager.getConnection(“jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8”, “root”, “19810109”);

String sql=”insert into book(title,price,birth,publish_date,update_date)values(?,?,?,?,?)”;

//预编译

PreparedStatement pstmt=conn.prepareStatement(sql);

pstmt.setString(1, “小说”);

pstmt.setFloat(2, 40.55f);

pstmt.setTimestamp(3, new Timestamp(new Date().getTime()));

pstmt.setTimestamp(4, new Timestamp(new Date().getTime()));

pstmt.setTimestamp(5, new Timestamp(new Date().getTime()));

//执行

pstmt.executeUpdate();

} catch (Exception e) {

e.printStackTrace();

}

finally

{

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

预编译方式  查找 book 记录 id=1 的数据

package com.jdbc4;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Timestamp;

import java.util.Date;

public class TestFind {

/**

* @param args

*/

public static void main(String[] args) {

Connection conn=null;

try {

Class.forName(“com.mysql.jdbc.Driver”);

//建立连接  ctrl+shfit +o

conn=DriverManager.getConnection(“jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8”, “root”, “19810109”);

String sql=”select * from book where id=?”;

//预编译

PreparedStatement pstmt=conn.prepareStatement(sql);

pstmt.setInt(1, 1);

//执行

ResultSet rs=pstmt.executeQuery();

while(rs.next())

{

int id=rs.getInt(“id”);

String title=rs.getString(“title”);

float price=rs.getFloat(“price”);

Date birth=rs.getDate(“birth”);

Timestamp publish_date=rs.getTimestamp(“publish_date”);

Timestamp update_date=rs.getTimestamp(“update_date”);

System.out.println(id+”,”+title+”,”+price+”,”+birth+”,”+publish_date+”,”+update_date);

}

} catch (Exception e) {

e.printStackTrace();

}

finally

{

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

预编译方式  修改 book 记录 id=1 的数据

package com.jdbc4;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import java.sql.Timestamp;

import java.util.Date;

public class TestUpdate {

/**

* @param args

*/

public static void main(String[] args) {

Connection conn=null;

try {

Class.forName(“com.mysql.jdbc.Driver”);

//建立连接  ctrl+shfit +o

conn=DriverManager.getConnection(“jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8”, “root”, “19810109”);

String sql=”update book set title=?,price=?,birth=?,publish_date=?,update_date=? where id=?”;

//预编译

PreparedStatement pstmt=conn.prepareStatement(sql);

pstmt.setString(1, “四集”);

pstmt.setFloat(2, 50.55f);

pstmt.setDate(3, new java.sql.Date(new Date().getTime()));

pstmt.setTimestamp(4, new Timestamp(new Date().getTime()));

pstmt.setTimestamp(5, new Timestamp(new Date().getTime()));

pstmt.setInt(6, 1);

//执行

pstmt.executeUpdate();

} catch (Exception e) {

e.printStackTrace();

}

finally

{

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

预编译方式  删除 book 记录 id=1 的数据

package com.jdbc4;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import java.sql.Timestamp;

import java.util.Date;

public class TestDelete {

/**

* @param args

*/

public static void main(String[] args) {

Connection conn=null;

try {

Class.forName(“com.mysql.jdbc.Driver”);

//建立连接  ctrl+shfit +o

conn=DriverManager.getConnection(“jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8”, “root”, “19810109”);

String sql=”delete from book where id=?”;

//预编译

PreparedStatement pstmt=conn.prepareStatement(sql);

pstmt.setInt(1, 1);

//执行

pstmt.executeUpdate();

} catch (Exception e) {

e.printStackTrace();

}

finally

{

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}