1、JDBC.java
package com.ace.service;
import java.sql.*;
public class JDBC {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ace", "root", "888888");
Statement statement = connection.createStatement();
int count_add = statement.executeUpdate("insert into student (id, name) values ('jdbc', 'JDBC新增')");
if (count_add > 0) {
System.out.println("新增成功");
}
int count_upd = statement.executeUpdate("update student set name = 'JDBC修改' where id = 'jdbc'");
if (count_upd > 0) {
System.out.println("修改成功");
}
int count_del = statement.executeUpdate("delete from student where id = 'jdbc'");
if (count_del > 0) {
System.out.println("删除成功");
}
ResultSet resultSet = statement.executeQuery("select * from student");
while (resultSet.next()) {
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println("姓名:" + name + ",年龄:" + age);
}
resultSet.close();
resultSet = null;
statement.close();
statement = null;
connection.close();
connection = null;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2、BaseDao.java
package com.ace.service;
import java.sql.*;
public class BaseDao {
private final String driver = "com.mysql.jdbc.Driver";
private final String url = "jdbc:mysql://localhost:3306/ace";
private final String username = "root";
private final String password = "888888";
private Connection connection = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public Connection getConnection() {
try {
Class.forName(this.driver);
this.connection = DriverManager.getConnection(this.url, this.username, this.password);
return connection;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public int executeUpdate(String sql, Object[] params) {
int count = -1;
try {
this.getConnection();
this.preparedStatement = this.connection.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i + 1, params[i]);
}
}
count = preparedStatement.executeUpdate();
this.close();
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
public ResultSet executeQuery(String sql, Object[] params) {
try {
this.getConnection();
preparedStatement = this.connection.prepareStatement(sql);
if (params != null) {
for (int i = 0; i < params.length; i++) {
preparedStatement.setObject(i + 1, params[i]);
}
}
resultSet = preparedStatement.executeQuery();
return resultSet;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public void close() {
try {
if (this.resultSet != null) {
this.resultSet.close();
this.resultSet = null;
}
if (this.preparedStatement != null) {
this.preparedStatement.close();
this.preparedStatement = null;
}
if (this.connection != null) {
this.connection.close();
this.connection = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String sql_add = "insert into student (id, name) values (?, ?)";
int count_add = new BaseDao().executeUpdate(sql_add, new Object[]{"basedao", "BaseDao新增"});
if (count_add > 0) {
System.out.println("新增成功");
}
String sql_upd = "update student set name = ? where id = ?";
int count_upd = new BaseDao().executeUpdate(sql_upd, new Object[]{"BaseDao修改", "basedao"});
if (count_upd > 0) {
System.out.println("修改成功");
}
String sql_del = "delete from student where id = ?";
int count_del = new BaseDao().executeUpdate(sql_del, new Object[]{"basedao"});
if (count_del > 0) {
System.out.println("删除成功");
}
String sql = "select * from student where name like ? and age = ?";
ResultSet resultSet = new BaseDao().executeQuery(sql, new Object[]{"%张三%", 20});
try {
while (resultSet.next()) {
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
System.out.println("姓名:" + name + ",年龄:" + age);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}