JDBC

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.PreparedStatement;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 
 7 public class JdbcUtil {
 8     
 9     private String driver="oracle.jdbc.driver.OracleDriver";
10     private String url="jdbc:oracle:thin:@localhost:8080: dbName";
11     private String username="username";
12     private String password="password";
13     
14     private Connection conn = null;
15     private PreparedStatement pstmt = null;
16     private ResultSet rst = null;
17     
18     public  UserEntity query(String value){
19         String sql = "select user_id,user_name,phone from p_m_user where user_id =" + value;
20         UserEntity user = new UserEntity();
21         
22         try {
23             Class.forName(driver);
24             conn = DriverManager.getConnection(url, username, password);
25             pstmt = conn.prepareStatement(sql);
26             rst = pstmt.executeQuery();
27             if(rst.next()){
28                 user.setUser_id(rst.getString("user_id"));
29                 user.setUser_name(rst.getString("user_name"));
30                 user.setPhone(rst.getNString("phone"));
31             }
32             rst.close();
33             pstmt.close();
34             conn.close();
35         } catch (ClassNotFoundException e) {
36             // TODO Auto-generated catch block
37             e.printStackTrace();
38         } catch(SQLException e2){
39             e2.printStackTrace();
40         }
41         return user;
42     }
43   

 

posted @ 2017-03-25 15:39  unique1319  阅读(158)  评论(0编辑  收藏  举报