创建工具类DBUtil
public class DBUtil {
public static Connection getConnection(){
//1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
Connection connection = DriverManager.getConnection
("jdbc:mysql://127.0.0.1:3306/user?useSSL=true&" +
"characterEncoding=utf-8&user=" +
"root&password=123");
return connection;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
对数据库进行查询操作
public class Test {
public static void main(String[] args) {
ResultSet rs = null;
Connection connection = null;
PreparedStatement statement = null;
try {
//1.2步
connection= DBUtil.getConnection();
System.out.println("创建连接成功");
//3.写sql查询语句
String sql = "select*from userinfo";
//4.得到statement对象
statement = connection.prepareStatement(sql);
//5.执行sql得到结果集
rs = statement.executeQuery();
//6.处理结果集
while (rs.next()) {
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
}
//7.关闭资源
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.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();
}
}
}
}
}
插入数据操作
public class Insert {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement statement = null;
try {
//1.2步
connection= DBUtil.getConnection();
//3.写sql查询语句
String sql = "insert into userinfo (username,password) values (?,?)";
//4.得到statement对象
statement = connection.prepareStatement(sql);
statement.setString(1,"dingshili");
statement.setString(2,"123");
statement.executeUpdate();
//5.执行sql得到结果集
// rs = statement.executeQuery();
//6.处理结果集
//while (rs.next()) {
// System.out.println(rs.getInt(1));
// System.out.println(rs.getString(2));
// System.out.println(rs.getString(3));
//}
//7.关闭资源
} catch (Exception e) {
e.printStackTrace();
} finally {
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
删除数据操作:
public class Delete {
public static void main(String[] args){
Connection connection = null;
PreparedStatement statement = null;
try {
//1.2步
connection= DBUtil.getConnection();
//3.写sql删除语句
String sql = "delete from userinfo where id='1'";
//4.得到statement对象
statement = connection.prepareStatement(sql);
//更新数据
statement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
修改数据操作:
public class Update {
public static void main(String[] args){
Connection connection = null;
PreparedStatement statement = null;
try {
//1.2步
connection= DBUtil.getConnection();
//3.写sql删除语句
String sql="update userinfo set id='9' where id='2'";
//4.得到statement对象
statement = connection.prepareStatement(sql);
//更新数据
statement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(statement!=null){
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}