zoukankan      html  css  js  c++  java
  • 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   
  • 相关阅读:
    linux 文件记录锁详解
    Linux fcntl函数详解
    大数相加
    信雅达面试题atoi函数实现
    linux getopt函数详解
    strcpy和memcpy的区别
    手把手写数据结构之栈操作
    手把手写数据结构之队列操作
    手把手写数据结构之双向链表操作
    ORACLE查询内存溢出
  • 原文地址:https://www.cnblogs.com/unique1319/p/6617313.html
Copyright © 2011-2022 走看看