1.登录案例
工具类
1 package day2_JDBC; 2
3 import java.sql.*;
4
5 /**
6 * JDBC工具类
7 */
8 public class JDBCUtils {
9 private static String driver = "com.mysql.jdbc.Driver";
10 private static String url = "jdbc:mysql://localhost/db3";
11 private static String user = "root";
12 private static String password = "root";
13
14 static {
15 try {
16 Class.forName(driver);
17 } catch (ClassNotFoundException e) {
18 e.printStackTrace();
19 }
20 }
21 /**
22 * 获取连接
23 *
24 * @return 连接对象
25 */
26 public static Connection getConnection() throws SQLException {
27 return DriverManager.getConnection(url,user,password);
28 }
29
30 /**
31 * 释放资源
32 *
33 * @param stmt
34 * @param conn
35 */
36 public static void close(Statement stmt, Connection conn) {
37 if (stmt != null) {
38 try {
39 stmt.close();
40 } catch (SQLException e) {
41 e.printStackTrace();
42 }
43 }
44 if (conn != null) {
45 try {
46 conn.close();
47 } catch (SQLException e) {
48 e.printStackTrace();
49 }
50 }
51 }
52
53 /**
54 * 重载close方法
55 * @param rs
56 * @param stmt
57 * @param conn
58 */
59 public static void close(ResultSet rs, Statement stmt, Connection conn) {
60 if (rs != null) {
61 try {
62 rs.close();
63 } catch (SQLException e) {
64 e.printStackTrace();
65 }
66 }
67 if (stmt != null) {
68 try {
69 stmt.close();
70 } catch (SQLException e) {
71 e.printStackTrace();
72 }
73 }
74 if (conn != null) {
75 try {
76 conn.close();
77 } catch (SQLException e) {
78 e.printStackTrace();
79 }
80 }
81 }
82 }
测试类
1 package day2_JDBC;
2
3 import java.beans.beancontext.BeanContext;
4 import java.sql.Connection;
5 import java.sql.PreparedStatement;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.util.Scanner;
9
10 public class LoginDemo {
11
12 public static void main(String[] args){
13 Scanner sc = new Scanner(System.in);
14 System.out.println("请输入用户名:");
15 String username = sc.next();
16 System.out.println("请输入密码:");
17 String password = sc.next();
18 boolean flag = new LoginDemo().login(username, password);
19 if(flag) {
20 System.out.println(username+"登录成功!");
21 }else {
22 System.out.println("用户名或密码错误!!!");
23 }
24 }
25
26 public boolean login(String username,String password) {
27 Connection conn = null;
28 PreparedStatement pstmt = null;
29 ResultSet rs = null;
30 if(username==null||password==null ) {
31 return false;
32 }
33 try {
34 //获取连接
35 conn = JDBCUtils.getConnection();
36 //获取执行sql对象
37 pstmt = conn.prepareStatement("select * from user where username=? and password=?");
38 pstmt.setString(1, username);
39 pstmt.setString(2, password);
40 //执行sql
41 rs = pstmt.executeQuery();
42 return rs.next(); //如果有结果返回true
43 } catch (SQLException e) {
44 // TODO Auto-generated catch block
45 e.printStackTrace();
46 }finally {
47 //释放资源
48 JDBCUtils.close(rs,pstmt,conn);
49 }
50 return false;
51 }
52
53 }
2.数据显示
实体类
1 package day2_JDBC;
2 /**
3 * 数据库实体类javaBean
4 * @author master
5 *
6 */
7 public class Acount {
8 private int id;
9 private String name;
10 private Double balance;
11
12
13 public int getId() {
14 return id;
15 }
16 public void setId(int id) {
17 this.id = id;
18 }
19 public String getName() {
20 return name;
21 }
22 public void setName(String name) {
23 this.name = name;
24 }
25 public Double getBalance() {
26 return balance;
27 }
28 public void setBalance(Double balance) {
29 this.balance = balance;
30 }
31 @Override
32 public String toString() {
33 return "Acount [id=" + id + ", name=" + name + ", balance=" + balance + "]";
34 }
35
36
37 }
测试类
1 package day2_JDBC;
2
3 import java.sql.DriverManager;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.sql.Connection;
10
11 public class Test {
12
13 public static void main(String[] args) {
14 Connection conn = null;
15 PreparedStatement pstmt = null;
16 ResultSet rs = null;
17 Acount at = null;
18 List<Acount> list = null;
19 try {
20 //注册驱动
21 Class.forName("com.mysql.jdbc.Driver");
22 System.out.println("驱动注册成功!");
23 //获取数据库连接
24 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","root");
25 System.out.println("数据库连接成功");
26 //定义sql
27 String sql = "select * from acount";
28 //获取执行sql对象
29 pstmt = conn.prepareStatement(sql);
30 //执行sql返回结果集
31 rs = pstmt.executeQuery();
32 System.out.println("id\tname\tbalance");
33 //实例化对象
34 at = new Acount();
35 list = new ArrayList<Acount>();
36 while(rs.next()) {
37 //获取数据
38 int id = rs.getInt(1);
39 String name = rs.getString(2);
40 Double balance = rs.getDouble(3);
41 System.out.println(id+"\t"+name+"\t"+balance);
42 //保存数据
43 at.setName(name);
44 at.setId(id);
45 at.setBalance(balance);
46 //保存到list
47 list.add(at);
48 }
49
50 System.out.println("length:"+list.size());
51 //遍历集合
52 for(Acount a:list) {
53 System.out.println(a.getId()+"\t"+a.getName()+"\t"+a.getBalance());
54 }
55 } catch (ClassNotFoundException e) {
56 System.out.println("驱动注册成功!");
57 e.printStackTrace();
58 } catch (SQLException e) {
59 System.out.println("数据库连接失败");
60 e.printStackTrace();
61 }finally {
62 if(rs!=null) {
63 try {
64 rs.close();
65 System.out.println("resultset已关闭");
66 } catch (SQLException e) {
67 // TODO Auto-generated catch block
68 e.printStackTrace();
69 }
70 }
71 if(pstmt!=null) {
72 try {
73 pstmt.close();
74 System.out.println("preparedstatment已关闭");
75 } catch (SQLException e) {
76 // TODO Auto-generated catch block
77 e.printStackTrace();
78 }
79 }
80 if(conn!=null) {
81 try {
82 conn.close();
83 System.out.println("connection已关闭");
84 } catch (SQLException e) {
85 // TODO Auto-generated catch block
86 e.printStackTrace();
87 }
88 }
89 }
90
91 }
92 }
来源:https://www.cnblogs.com/987m/p/12188531.html