路转峰回出画塘,一山枫叶背残阳。——《浣溪沙》
前一篇,是我第一次用java连接mysql数据库。这一篇,主要介绍怎样在java中使用SQL语句,对数据库进行“增删改查“这四个操作,也算是对MySQL数据库的深入学习。
假设我现在有一张表(LoginTable),这张表是用户登录账号密码的记录,有 账号(zhanghao)和密码(password)
insert into LoginTable(zhanghao,password) values(?,?)
delete from LoginTable where zhanghao=?
update LoginTable set password=? where zhanghao=?
select * from LoginTable
因为java要操作MySQL数据库首先得建立连接,所以我们建立一个专门用来获取连接和关闭连接的专用功能类(JDBCUtil)
package Login2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCUtil {
public static Connection getconnection() {
String connetURL="jdbc:mysql://127.0.0.1:3306/test?useSSL=false";
String connetZHANGHAO="root";
String connetPASSWORD="root";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection=DriverManager.getConnection(connetURL,connetZHANGHAO,connetPASSWORD);
return connection;
}catch (Exception e) {
System.out.println("数据库断开连接");
}
return null;
}
public static void closeOpen(ResultSet resultSet,PreparedStatement preparedStatement,Connection connection) {
if(resultSet!=null)
try {
resultSet.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(preparedStatement!=null)
try {
preparedStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(connection!=null)
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package Login2;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class AddDeleteUpdaeShow {
public static void add(String zhanghao,String password) {
Connection connection=JDBCUtil.getconnection();
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
int flag=0;
String sql="insert into mountpasswordtable(zhanghao,password) values(?,?)";
try {
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1, zhanghao);
preparedStatement.setString(2, password);
flag=preparedStatement.executeUpdate();
if(flag==1) {
System.out.println("sucess");
}else {
System.out.println("fail");
}
} catch (Exception e) {
System.out.println("error:");
System.out.println("账号重复");
}finally {
JDBCUtil.closeOpen(resultSet, preparedStatement, connection);
}
}
public static void delete(String zhanghao) {
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
connection=JDBCUtil.getconnection();
String sql="delete from mountpasswordtable where zhanghao=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1, zhanghao);
int number=preparedStatement.executeUpdate();
if(number>0) {
System.out.println("sucess");
}else {
System.out.println("fail");
}
} catch (Exception e) {
System.out.println("error");
}finally {
JDBCUtil.closeOpen(resultSet, preparedStatement, connection);
}
}
public static void update(String zhanghao,String password) {
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
connection=JDBCUtil.getconnection();
String sql="update mountpasswordtable set password=? where zhanghao=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1, password);//这里的setString是?的值
preparedStatement.setString(2, zhanghao);
int number=preparedStatement.executeUpdate();
if(number>0) {
System.out.println("sucess");
}else {
System.out.println("fail");
}
} catch (Exception e) {
System.out.println("error");
}finally {
JDBCUtil.closeOpen(resultSet, preparedStatement, connection);
}
}
public static void show() {
Connection connection=null;
PreparedStatement preparedStatement = null;
ResultSet resultSet=null;
try {
connection=JDBCUtil.getconnection();
String sql="select * from mountpasswordtable";
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
while(resultSet.next()) {
System.out.println(resultSet.getInt(1)+"/"+resultSet.getString(2)+"/"+resultSet.getString(3));
}
} catch (Exception e) {
System.out.println("error");
}finally {
JDBCUtil.closeOpen(resultSet, preparedStatement, connection);
}
}
}
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useSSL=false", "root", "root");
statement=connection.createStatement();
resultSet=statement.executeQuery("select * from logininfo where zhanghao='"+zhanghao+"' and password='"+password+"'");
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useSSL=false", "root", "root");
preparedStatement=statement=connection.prepareStatement("select * from logininfo where zhanghao=? and password=?");
preparedStatement.setString(1, zhanghao);
preparedStatement.setString(2, password);
resultSet=preparedStatement.executeQuery();
JDBCUtil这种专门建立一个常用类的思想很好,经过封装,我们就能简单调用,减少了代码冗余,也使得代码整洁易懂。对数据库的增删改查学习,是对JDBC连接数据库的深入,为接下来JavaWeb学习打下一个基础。