package cn.itcat.domain;
import com.sun.org.apache.xpath.internal.objects.XString;
import java.util.Date;
public class Emp {
private int id;
private String ename;
private Date jiondate;
private double salary;
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", ename='" + ename + '\'' +
", jiondate=" + jiondate +
", salary=" + salary +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Date getJiondate() {
return jiondate;
}
public void setJiondate(Date jiondate) {
this.jiondate = jiondate;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
package cn.itcat.jdbc;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
private static String user;
private static String password;
private static String url;
private static String drive;
static {
//读取资源文件
try {
//1.创建Propeties集合类
Properties pro=new Properties();
//获取src路径下文件的方式--->ClassLoader
ClassLoader classLoader=JDBCUtils.class.getClassLoader();
URL res=classLoader.getResource("jdbc.properties");
String path = res.getPath();
System.out.println(path);
//2.加载文件
//pro.load(new FileReader("D:\\eclipse-workspace\\jdbc\\day0_jdbc\\src\\jdbc.properties"));
pro.load(new FileReader(path));
//3.获取数据,赋值
url=pro.getProperty("url");
user=pro.getProperty("user");
password=pro.getProperty("password");
drive=pro.getProperty("driver");
//4.注册驱动
try {
Class.forName(drive);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 文件的读取,只需要读取一次即可拿到这些值
* */
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
//施放资源
public static void close(Statement stmt,Connection conn){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(ResultSet rs, Statement stmt, Connection conn){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package cn.itcat.jdbc;
import cn.itcat.domain.Emp;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class jdbc8UsUtils {
public static void main(String[] args) {
List<Emp> list = new jdbcSelectJson().findAll();
System.out.println(list);
System.out.println("总共"+list.size()+"条");
}
/*
* 查询所有emp对象
**/
public List<Emp> findAll(){
ResultSet rs=null;
Statement stmt=null;
Connection conn=null;
List<Emp> list=null;
try {
//1.注册驱动
// Class.forName("com.mysql.jdbc.Driver");
try {
//2.获取连接
// conn= DriverManager.getConnection("jdbc:mysql:///db3","root","root");
//3.定义sql
conn= JDBCUtils.getConnection();
String sql="select * from emp";
//4.获取执行sql的对象
stmt = conn.createStatement();
//5.执行sql
rs=stmt.executeQuery(sql);
//6.遍历结果集,封装结果,装载结果
Emp emp=null;
list=new ArrayList<Emp>();
while(rs.next()){
int id = rs.getInt("id");
String ename = rs.getString("ename");
Date jiondate = rs.getDate("joindate");
double salary = rs.getDouble("salary");
emp=new Emp();
emp.setId(id);
emp.setEname(ename);
emp.setJiondate(jiondate);
emp.setSalary(salary);
//装载集合
list.add(emp);
}
} catch (SQLException e) {
e.printStackTrace();
}
}finally {
JDBCUtils.close(rs,stmt,conn);
}
return list;
}
}
package cn.itcat.jdbc;
import java.sql.*;
import java.util.Scanner;
/*
* 使用PrepardStatement
* */
public class jdbcPrepardStatement {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入用户名:");
String username=sc.nextLine();
System.out.println("请输入密码:");
String password=sc.nextLine();
boolean flat = new jdbcPrepardStatement().login(username, password);
if (flat){
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
}
/*
* 登陆方法
* */
public boolean login(String username,String password) {
if (username == null || password == null) {
return false;
}
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
conn = JDBCUtils.getConnection();
String sql="select * from user where username= ? and password= ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,username);
pstmt.setString(2,password);
rs = pstmt.executeQuery();
return rs.next();//next()返回值本身就位布尔型
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.close(rs,pstmt,conn);
}
return true;
}
}
package cn.itcat.jdbc;
import java.sql.*;
import java.util.Scanner;
/*
* 使用PrepardStatement
* */
public class jdbcPrepardStatement {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入用户名:");
String username=sc.nextLine();
System.out.println("请输入密码:");
String password=sc.nextLine();
boolean flat = new jdbcPrepardStatement().login(username, password);
if (flat){
System.out.println("登录成功");
}else {
System.out.println("登录失败");
}
}
/*
* 登陆方法
* */
public boolean login(String username,String password) {
if (username == null || password == null) {
return false;
}
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
conn = JDBCUtils.getConnection();
String sql="select * from user where username= ? and password= ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,username);
pstmt.setString(2,password);
rs = pstmt.executeQuery();
return rs.next();//next()返回值本身就位布尔型
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtils.close(rs,pstmt,conn);
}
return true;
}
}