本文共 3473 字,大约阅读时间需要 11 分钟。
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;public classJDBC01 {public static void main(String[] args) throwsSQLException {
selectAll();
}public static void selectAll() throwsSQLException {//注册驱动 使用驱动连接数据库
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)+","+rs.getString(2)+","+rs.getString(3));
System.out.println(rs.getString("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) throwsSQLException {
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","123456");
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) throwsSQLException{
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","123456");
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) throwsSQLException {//注册驱动 使用驱动连接数据库
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)+","+rs.getString(2)+","+rs.getString(3));
System.out.println(rs.getString("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)
stmt.close();if(con!=null)
con.close();
}
}
}
转载地址:https://blog.csdn.net/weixin_32573931/article/details/114314341 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!