JDBC学习(含转载)
时间:2021-07-01 10:21:17
帮助过:2人阅读
import java.sql.*
;
2 public class mypreparedstatement {
3 private final string db_driver="com.microsoft.jdbc.sqlserver.sqlserverdriver"
;
4 private final string url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs"
;
5 public mypreparedstatement()
6 {
7 }
8 public void query()
throws sqlexception{
9 connection conn =
this.getconnection();
10 string strsql = "select emp_id from employee where emp_id = ?"
;
11 preparedstatement pstmt =
conn.preparestatement(strsql);
12 pstmt.setstring(1,"pma42628m"
);
13 resultset rs =
pstmt.executequery();
14
15 while(rs.next()){
16 string fname = rs.getstring("emp_id"
);
17 system.out.println("the fname is " +
fname);
18 }
19 rs.close();
20 pstmt.close();
21 conn.close();
22 }
23 private connection getconnection()
throws sqlexception{
24 // class.
25 connection conn =
null;
26 try {
27 class.forname(db_driver);
28 conn = drivermanager.getconnection(url,"sa","sa"
);
29 }
30 catch (classnotfoundexception ex) {}
31 return conn;
32 }
33 //main
34 public static void main(string[] args)
throws sqlexception {
35 mypreparedstatement jdbctest1 =
new mypreparedstatement();
36 jdbctest1.query();
37 }
38 }
JDBC学习(含转载)
标签: