2017-07-12 17:14:10
1 package com.zczr.j03.jdbc;
2
3
4 import java.io.BufferedReader;
5 import java.io.InputStreamReader;
6 import java.sql.Connection;
7 import java.sql.DriverManager;
8 import java.sql.PreparedStatement;
9 import java.sql.ResultSet;
10
11
12
13 public class JDBC_Test02 {
14
15 static Connection conn = null;
16 static PreparedStatement ps = null;
17 static ResultSet rs = null;
18
19 public static void main(String[] args) throws Exception {
20 // InputStreamReader ir = new InputStreamReader(System.in);
21 // BufferedReader bf = new BufferedReader(ir);
22 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
23 System.out.println("请输入年龄:");
24 int inputAge = Integer.parseInt(br.readLine());
25
26 Class.forName("com.mysql.jdbc.Driver");
27
28 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/java03", "root", "1234");
29
30 String sql = "select id, name from student where age > ?";
31 ps = conn.prepareStatement(sql);
32 ps.setInt(1, inputAge);
33
34 rs = ps.executeQuery();
35 while(rs.next()) {
36 System.out.print(rs.getInt("id")+ " ");
37 System.out.print(rs.getString("name") + " ");
38 System.out.println();
39 }
40 rs.close();
41 ps.close();
42 conn.close();
43 }
44
45 }