MySQL_(Java)使⽤JDBC向数据库中修改(update)数据
  MySQL_(Java)使⽤JDBC向数据库发起查询请求 
  MySQL_(Java)使⽤JDBC向数据库中插⼊(insert)数据 
  MySQL_(Java)使⽤JDBC向数据库中删除(delete)数据 
  MySQL_(Java)使⽤JDBC向数据库中修改(update)数据 
  MySQL数据库中的数据,数据库名garysql,表名garytb,修改id=1⽤户名密码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBC01 {
public static void main(String[] args) throws SQLException  {
update(1,"111");
}
public static void selectAll() throws SQLException {
//注册驱动使⽤驱动连接数据库
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
//数据库的连接
//数据库的连接
con = Connection();
//数据库的增删改查
stmt = ateStatement();
//返回⼀个结果集
rs =uteQuery("select * from garytb");
()) {
//System.out.String(1)+","+rs.getString(2)+","+rs.getString(3));
System.out.String("id")+","+rs.getString("username")+","+rs.getString("password"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.close(rs, stmt, con);
}
}
//校验⽤户
public static boolean  selectByUernamePassword(String username,String password) throws SQLException {
Connection con=null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("sql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";            con = Connection(url,"root","123456");
stmt =ateStatement();
String sql = "select * from garytb where username = '"+username+"' and password = '"+password+"'";
//System.out.println(sql);
rs = uteQuery(sql);
()) {
return true;
}else {
return false;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
return false;
}
public static boolean selectByUP2(String username,String password) throws SQLException{
Connection con=null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("sql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";            con = Connection(url,"root","123456");
con = Connection(url,"root","123456");
String sql = "select * from garytb where username = ? and password = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
//添加参数
pstmt.setString(1, username);
pstmt.setString(2, password);
//进⾏查询
rs = uteQuery();
()) {
return true;
}else {
return false;
}
sql中update什么意思} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
return false;
}
//pageNumber是页数,第⼏页,pageCount是每页显⽰多少个数据
public static void selectUserByPage(int pageNumber,int pageCount) throws SQLException {
//注册驱动使⽤驱动连接数据库
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Class.forName("sql.jdbc.Driver");
/
/String url ="jdbc:mysql://localhost:3306/garysql";
//指定编码查询数据库
String url ="jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";                    String user = "root";
String password = "123456";
//建⽴和数据库的连接
con = Connection(url,user,password);
stmt = con.prepareStatement("select * from garytb limit ?,?");
stmt.setInt(1, (pageNumber-1)*pageCount );
stmt.setInt(2, pageCount);
rs = uteQuery();
()) {
//System.out.String(1)+","+rs.getString(2)+","+rs.getString(3));
System.out.String("id")+","+rs.getString("username")+","+rs.getString("password"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(rs!=null)
rs.close();
if(stmt!=null)
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
}
//crud: create read update delete
//插⼊语句
public static void insert(String username,String password) throws SQLException { //注册驱动使⽤驱动连接数据库
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = Connection();
String sql = "insert into garytb(username,password) values(?,?)";
stmt = con.prepareStatement(sql);
stmt.setString(1, username);
stmt.setString(2, password);
int result =uteUpdate();// 返回值代表收到影响的⾏数
System.out.println("插⼊成功"+username);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtils.close(rs, stmt, con);
}
}
//删除语句
public static void delete(int id) throws SQLException {
//注册驱动使⽤驱动连接数据库
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = Connection();
String sql = "delete from garytb where id = ?";
stmt = con.prepareStatement(sql);
stmt.setInt(1, id);
int result =uteUpdate();// 返回值代表收到影响的⾏数
if(result>0) {
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(rs, stmt, con);
}
}
//修改语句
public static void update(int id,String newPassword) throws SQLException {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = Connection();
String sql = "update garytb set password = ? where id = ?";
stmt = con.prepareStatement(sql);
stmt.setString(1, newPassword);
stmt.setInt(2, id);
int result =uteUpdate();// 返回值代表收到影响的⾏数
int result =uteUpdate();// 返回值代表收到影响的⾏数
if(result>0) {
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.close(rs, stmt, con);
}
}
}
JDBC01.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtils {
private static final String connectionURL = "jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false";
private static final String username = "root";
private static final String password = "123";
//创建数据库的连接
public static Connection getConnection() {
try {
Class.forName("sql.jdbc.Driver");
return  Connection(connectionURL,username,password);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//关闭数据库的连接
public static void close(ResultSet rs,Statement stmt,Connection con) throws SQLException {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(con!=null)
con.close();
}
}
JDBCUtils.java
  根据id修改数据库中的数据

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。