您的位置:首页 > 数据库 > MySQL

分别使用PreparedStatement和Statement对mysql数据库进行创建表,增加数据,查询数据和删除数据过程

2017-11-11 16:36 1016 查看
在使用eclipse工具编写Java代码连接数据库并对数据库进行处理时,总会用到对数据的增删改查操作。那么这个

时候就用到了java自带的sql库中的PreparedStatement或者Statement了。

其实PreparedStatement和Statement使用起来很相似,我认为二者之间的不同点也就是PreparedStatements可以

对数据进行批处理(使用addBatch()方法),而Statement只能对单个数据进行处理。

我将两种写法都写到同一个类中通过主函数调用的方法实现,具体代码如下(完整代码,经测试):

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

public class PutinStorage {
//使用PreparedStatement对mysql数据库进行创建表,增加数据,查询数据和删除数据过程
public static void process1(){
System.out.println("process1");
String sql_url = "jdbc:mysql://localhost:3306/test";	//数据库路径(一般都是这样写),test是数据库名称
String name = "root";		//用户名
String password = "123456";	//密码
Connection conn;
PreparedStatement preparedStatement = null;

try {
Class.forName("com.mysql.jdbc.Driver");		//连接驱动
conn = DriverManager.getConnection(sql_url, name, password);	//连接数据库
if(!conn.isClosed())
System.out.println("成功连接数据库");
//新建表
String sql = "create table aa(id int,name text)";
preparedStatement = conn.prepareStatement(sql);
preparedStatement.executeUpdate();

//在表中添加内容
//			preparedStatement.executeUpdate("insert into aa values(4,'amy')");

preparedStatement = conn.prepareStatement("insert into aa values(1,'张三')");
preparedStatement.executeUpdate();
preparedStatement = conn.prepareStatement("insert into aa values(2,'李四')");
preparedStatement.executeUpdate();
preparedStatement = conn.prepareStatement("insert into aa values(3,'王五')");
preparedStatement.executeUpdate();

//查询表内容
System.out.println("第一次查询表内容(删除前)");
preparedStatement = conn.prepareStatement("select * from aa");
ResultSet result1 = preparedStatement.executeQuery();
while(result1.next())
System.out.println(result1.getInt("id")+"\t"+result1.getString("name"));

//删除表中数据
preparedStatement = conn.prepareStatement("delete from aa where id = 2");
preparedStatement.executeUpdate();

//查询表中内容
System.out.println("第二次查询表内容(删除后)");
preparedStatement = conn.prepareStatement("select * from aa");
ResultSet result2 = preparedStatement.executeQuery();
while(result2.next())
System.out.println(result2.getInt("id")+"\t"+result2.getString("name"));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("未成功加载驱动。");
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("未成功打开数据库。");
e.printStackTrace();
}
}

//使用Statement对mysql数据库进行创建表,增加数据,查询数据和删除数据过程
public static void process2(){
System.out.println("process2");
String sql_url = "jdbc:mysql://localhost:3306/test";	//数据库路径(一般都是这样写),test是数据库名称
String name = "root";		//用户名
String password = "123456";	//密码
Connection conn;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");		//连接驱动
conn = DriverManager.getConnection(sql_url, name, password);	//连接数据库
if(!conn.isClosed())
System.out.println("成功连接数据库");
statement = conn.createStatement();
//新建表
String sql = "create table bb(id int,name text)";
statement.executeUpdate(sql);

//在表中添加内容
statement.executeUpdate("insert into bb values(1,'张三')");
statement.executeUpdate("insert into bb values(2,'李四')");
statement.executeUpdate("insert into bb values(3,'王五')");

//查询表内容
System.out.println("第一次查询表内容(删除前)");
ResultSet result1 = statement.executeQuery("select * from bb");
while(result1.next())
System.out.println(result1.getInt("id")+"\t"+result1.getString("name"));

//删除表中数据
statement.executeUpdate("delete from bb where id = 2");

//查询表内容
System.out.println("第二次查询表内容(删除后)");
ResultSet result2 = statement.executeQuery("select * from bb");
while(result2.next())
System.out.println(result2.getInt("id")+"\t"+result2.getString("name"));

} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("未成功加载驱动。");
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("未成功打开数据库。");
e.printStackTrace();
}
}

public static void main(String[] args){
process1();
//		process2();
}
}
其中使用PreparedStatement时的preparedStatement.executeUpdate("insert into aa values(4,'amy')");语句与

preparedStatement = conn.prepareStatement("insert into aa values(1,'张三')");
preparedStatement.executeUpdate();
所表达的含义相同。
执行结果分别为:

(1)、process1:

process1

成功连接数据库

第一次查询表内容(删除前)

1 张三

2 李四

3 王五

第二次查询表内容(删除后)

1 张三

3 王五

(2)、process2:

process2

成功连接数据库

第一次查询表内容(删除前)

1 张三

2 李四

3 王五

第二次查询表内容(删除后)

1 张三

3 王五
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐