数据库查询(二)
时间:2021-07-01 10:21:17
帮助过:11人阅读
package com.JDBC.proc;
2
3 import java.sql.*
;
4
5 public class PrepareStatement {
6 private static final String DBDRIVER = "com.mysql.jdbc.Driver"
;
7 private static final String DBURL = "jdbc:mysql://localhost:3306/mldn"
;
8 private static final String DBUSER = "root"
;
9 private static final String DBPASS = "root"
;
10 public static void main(String[] args)
throws Exception{
11
12 Connection conn=
null;
13 PreparedStatement psmt=
null;
14 ResultSet rs=
null;
15 Class.forName(DBDRIVER);
16
17 conn =
DriverManager.getConnection(DBURL, DBUSER, DBPASS);
18 String sql="select empno,ename " + "from emp " + "where empno=?"
;
19 psmt=
conn.prepareStatement(sql);
20 psmt.setInt(1, 123
);
21
22 rs=
psmt.executeQuery();
23 while(rs.next()){
24 System.out.println(rs.getInt(1)+"---"+rs.getString(2
));
25 }
26 rs.close();
27 psmt.close();
28 conn.close();
29
30 }
31 }
数据库查询(二)
标签:jdb connect row bpa final class ext use nbsp