MySQL_(Java)使⽤JDBC向数据库发起查询请求 MySQL_(Java)使⽤JDBC向数据库中插⼊(insert)数据 MySQL_(Java)使⽤JDBC向数据库中删除(delete)数据 MySQL_(Java)使⽤JDBC向数据库中修改(update)数据
MySQL数据库中的数据,数据库名garysql,表名garytb,删除数据库id=2的数据
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 { delete(2); }
public static void selectAll() throws SQLException { //注册驱动 使⽤驱动连接数据库 Connection con = null; Statement stmt = null; ResultSet rs = null; try {
//数据库的连接
con = JDBCUtils.getConnection();
//数据库的增删改查
stmt = con.createStatement(); //返回⼀个结果集
rs =stmt.executeQuery(\"select * from garytb\");
while(rs.next()) {
//System.out.println(rs.getString(1)+\
System.out.println(rs.getString(\"id\")+\ }
} 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(\"com.mysql.jdbc.Driver\");
String url =\"jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false\"; con = DriverManager.getConnection(url,\"root\ stmt =con.createStatement();
String sql = \"select * from garytb where username = '\"+username+\"' and password = '\"+password+\"'\"; //System.out.println(sql); rs = stmt.executeQuery(sql);
if(rs.next()) { 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(\"com.mysql.jdbc.Driver\");
String url =\"jdbc:mysql://localhost:3306/garysql?useUnicode=true&characterEncoding=UTF8&useSSL=false\"; con = DriverManager.getConnection(url,\"root\
String sql = \"select * from garytb where username = ? and password = ?\"; PreparedStatement pstmt = con.prepareStatement(sql); //添加参数
pstmt.setString(1, username); pstmt.setString(2, password); //进⾏查询
rs = pstmt.executeQuery();
if(rs.next()) { 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; }
//pageNumber是页数,第⼏页,pageCount是每页显⽰多少个数据
public static void selectUserByPage(int pageNumber,int pageCount) throws SQLException { //注册驱动 使⽤驱动连接数据库 Connection con = null;
PreparedStatement stmt = null; ResultSet rs = null; try {
Class.forName(\"com.mysql.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 = DriverManager.getConnection(url,user,password);
stmt = con.prepareStatement(\"select * from garytb limit ?,?\"); stmt.setInt(1, (pageNumber-1)*pageCount ); stmt.setInt(2, pageCount);
rs = stmt.executeQuery();
while(rs.next()) {
//System.out.println(rs.getString(1)+\
System.out.println(rs.getString(\"id\")+\
}
} 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(); } }
//crud: create read updata delete //插⼊语句
public static void insert(String username,String password) throws SQLException { //注册驱动 使⽤驱动连接数据库 Connection con = null;
PreparedStatement stmt = null; ResultSet rs = null; try {
con = JDBCUtils.getConnection();
String sql = \"insert into garytb(username,password) values(?,?)\"; stmt = con.prepareStatement(sql); stmt.setString(1, username); stmt.setString(2, password);
int result =stmt.executeUpdate();// 返回值代表收到影响的⾏数 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 = JDBCUtils.getConnection();
String sql = \"delete from garytb where id = ?\"; stmt = con.prepareStatement(sql); stmt.setInt(1, id);
int result =stmt.executeUpdate();// 返回值代表收到影响的⾏数 if(result>0) {
System.out.println(\"删除成功\"); }else {
System.out.println(\"删除失败\"); }
} catch (Exception e) { e.printStackTrace(); } finally {
JDBCUtils.close(rs, stmt, con); } }}
JDBC01.java
根据id删除数据库中的数据表
public static void delete(int id) throws SQLException { //注册驱动 使⽤驱动连接数据库 Connection con = null;
PreparedStatement stmt = null; ResultSet rs = null; try {
con = JDBCUtils.getConnection();
String sql = \"delete from garytb where id = ?\"; stmt = con.prepareStatement(sql); stmt.setInt(1, id);
int result =stmt.executeUpdate();// 返回值代表收到影响的⾏数 if(result>0) {
System.out.println(\"删除成功\");
}else {
System.out.println(\"删除失败\"); }
} catch (Exception e) { e.printStackTrace(); } finally {
JDBCUtils.close(rs, stmt, con); } }
因篇幅问题不能全部显示,请点此查看更多更全内容