java数据库的增删改查
增加功能实现的代码
package com.zr;
import com.zr.utl.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Add {
public static void main(String[] args) {
Connection connection= null;
try {
connection = DBUtil.getConnection();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
String sql="insert into tb_user values('5','ming','123456')";
PreparedStatement statement= null;
try {
statement = connection.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
try {
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
try {
DBUtil.closeAll(null,statement,connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
删除功能实现的代码
package com.zr;
import com.zr.utl.DBUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Delete {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection connection= DBUtil.getConnection();
System.out.println("创建连接成功");
String sql="delete from tb_user where id=3";
PreparedStatement statement=connection.prepareStatement(sql);
statement.executeQuery();
DBUtil.closeAll(null,statement,connection);
}
}
查找功能实现的代码
package com.zr;
import com.zr.utl.DBUtil;
import java.sql.*;
public class Find {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection connection= DBUtil.getConnection();
String sql = "select *from tb_user";
PreparedStatement statement=connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();
while (resultSet.next()) {
System.out.println(resultSet.getInt(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getString(3));
}
DBUtil.closeAll(resultSet,statement,connection);
}
}
修改功能实现的代码
package com.zr;
import com.zr.utl.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Update {
public static void main(String[] args) {
Connection connection= null;
try {
connection = DBUtil.getConnection();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
String sql="Update tb_user set password='111111' where id=3";
PreparedStatement statement= null;
try {
statement = connection.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
try {
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
try {
DBUtil.closeAll(null,statement,connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
数据库代码
package com.zr.utl;
import java.sql.*;
public class DBUtil {
public static Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/wzxxy","root","123456");
return connection;
}
public static void closeAll(ResultSet resultSet, Statement statement,Connection connection)throws SQLException{
resultSet.close();
}
}