Mysql学习笔记

2018-03-15  本文已影响0人  二十四小时爱你

Mysql与JDBC学习笔记(十)

实现对数据库数据的增删改查

前面已经对jdbc有一定的理解,接下来就可以使用jdbc来对数据库进行操作了。这里为了方便,把对数据库的连接和操作进行封装。

(一)创建MysqlUtil

public class MySqlUtil {

//1、加载驱动

static {

try {

Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

//2、获取连接

public static Connection getConnection(String url, String user, String password) {

Connection connection = null;

try {

connection = DriverManager.getConnection(url, user, password);

} catch (SQLException e) {

e.printStackTrace();

}

return connection;

}

//3、创建 Statement 对象

public static Statement getStatement(Connection connection) {

Statement statement = null;

try {

statement = connection.createStatement();

} catch (SQLException e) {

e.printStackTrace();

}

return statement;

}

//4、执行SQL语句并处理结果集( ResultSet )

public static ResultSet getResultSet(Statement statement, String sql) {

ResultSet resultSet = null;

try {

resultSet = statement.executeQuery(sql);

} catch (SQLException e) {

e.printStackTrace();

}

return resultSet;

}

//5、关闭连接

public static void close(Connection connection, Statement statement, ResultSet resultSet) {

if (resultSet != null) {

try {

resultSet.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (statement != null) {

try {

statement.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (connection != null) {

try {

connection.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

//6、 数据库的连接配置信息

public static String getProperty(String key) {

Properties properties = new Properties();

try {

properties.load(new FileInputStream("jdbc.properties"));

return properties.getProperty(key);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return "";

}

}

(二)对数据操作

(1)查询数据

//使用 PreparedStatement

public static void querydata(Connection connection,PreparedStatement preparedStatement) {// 查询

String sql = "select * from student where sno = ? ";

try {

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setString(1, "s011");

ResultSet rSet = preparedStatement.executeQuery();//返回的是一个结果集

while (rSet.next()) { //判断有没有下一条

String result = rSet.getString(1) + "," + rSet.getString(2) + "," + rSet.getString(3) + ","

+ rSet.getString(4);

System.out.println(result);

}

rSet.close();

preparedStatement.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

(2)增加数据

public static void adddata(Connection connection,PreparedStatement preparedStatement) {// 插入数据

String sql = "insert into student value(?,?,?,?)";

try {

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setString(1, "s011");//这里的1表示要插入的第1字段数据

preparedStatement.setString(2, "Tom"); //这里的2表示要插入的第2 字段数据

preparedStatement.setString(3, "21"); //这里的3表示要插入的第3字段数据

preparedStatement.setString(4, "男"); //这里的4表示要插入的第4字段数据

int result = preparedStatement.executeUpdate(); //result返回的结果是对几条数据操作,若大于0,则表示操作成功。

if (result > 0) {

System.out.println("插入成功");

} else {

System.out.println("插入失败");

}

preparedStatement.close();//关闭连接

} catch (SQLException e) {

e.printStackTrace();

}

}

(3)修改数据

public static void updatedata(Connection connection,PreparedStatement preparedStatement) {// 更新数据

String sql = "update student set sname=?,sage=? where sno='s011'";

try {

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setString(1, "Sun"); //这里的1表示要修改的第1字段数据

preparedStatement.setString(2, "19"); //这里的2表示要 修改 的第2字段数据

int result = preparedStatement.executeUpdate(); //result返回的结果是对几条数据操作,若大于0,则表示操作成功。

if (result > 0) {

System.out.println("更新成功");

} else {

System.out.println("更新失败");

}

preparedStatement.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

(4)删除数据

public static void deletedata(Connection connection,PreparedStatement preparedStatement) {// 删除数据

String sql = "delete from student where sno =?";

try {

preparedStatement = connection.prepareStatement(sql);

preparedStatement.setString(1, "s011"); //这里的1表示要删除的第1字段数据

int result = preparedStatement.executeUpdate(); //result返回的结果是对几条数据操作,若大于0,则表示操作成功。

if (result > 0) {

System.out.println("删除成功");

} else {

System.out.println("删除失败");

}

preparedStatement.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

(5)使用例子

public static void main(String[] args) {

String url = "jdbc:mysql://localhost:3306/studentdata?useSSL=false";

String user = "root";

String password = "";

Connection connection = MySqlUtil.getConnection(url, user, password);//获取连接数据库连接

PreparedStatement preparedStatement = null;

querydata(connection,preparedStatement);//查询数据

adddata(connection,preparedStatement);//添加数据

querydata(connection,preparedStatement); //查询数据

updatedata(connection,preparedStatement);//修改数据

querydata(connection,preparedStatement); //查询数据

deletedata(connection,preparedStatement);//删除数据

querydata(connection,preparedStatement); //查询数据

try {

connection.close();//关闭连接

} catch (SQLException e) {

e.printStackTrace();

}

}

jdbc--1

注:此例子所用的数据库在Mysql笔记(三)

上一篇 下一篇

猜你喜欢

热点阅读