JDBC 實戰應用,jdbc實戰應用

來源:互聯網
上載者:User

JDBC 實戰應用,jdbc實戰應用
1、JDBC 的使用順序為:        1)註冊驅動 (只做一次)         2)建立串連(Connection)       3)建立執行SQL的語句(Statement)        4)處理執行結果(ResultSet)        5)釋放資源

2、實戰應用

1)封裝資料庫連接字串/** * JDBC配置資訊 */public class JdbcConfig {    private String driverName;    private String url;    private String userName;    private String password;        public String getDriverName() {        return driverName;    }    public void setDriverName(String driverName) {        this.driverName = driverName;    }    public String getUrl() {        return url;    }    public void setUrl(String url) {        this.url = url;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}

        2)封裝資料庫常用操作
public class DbUtil {/**     * 取得Connection     * @return     */    public static Connection getConnection(){ Connection conn=null;        try {              //通過讀取設定檔擷取資料庫連接字串            JdbcConfig jdbcConfig=XmlConfigReader.getInstance().getJdbcConfig();              //載入驅動
            Class.forName(jdbcConfig.getDriverName());            String url=jdbcConfig.getUrl();            String userName=jdbcConfig.getUserName();            String password=jdbcConfig.getPassword();                  //建立串連            conn = (Connection) DriverManager.getConnection(url, userName, password);        } catch (ClassNotFoundException e) {            e.printStackTrace();        } catch (SQLException e) {            e.printStackTrace();        }        return conn;}}3)完成使用者的添加操作public class UserManager {    /**
     * 添加使用者     */    public void addUser(User user){        //建立SQL語句        String sql="insert into t_user(user_id,user_name,password,contact_tel,email,create_date)"+                    " values(?,?,?,?,?,?)";                Connection conn=null;        PreparedStatement pstmt=null;        try{            //擷取Connection            conn=DbUtil.getConnection();            pstmt=conn.prepareStatement(sql);                        //設定參數(這種方式可防止sql注入)            pstmt.setString(1, user.getUserId());            pstmt.setString(2, user.getUserName());            pstmt.setString(3, user.getPassword());            pstmt.setString(4, user.getContactTel());            pstmt.setString(5, user.getEmail());            pstmt.setTimestamp(6, new Timestamp(System.currentTimeMillis()));                        //執行使用者添加操作            pstmt.executeUpdate();        }catch(SQLException e){            e.printStackTrace();        }finally{            DbUtil.close(pstmt);            DbUtil.close(conn);        }    }}  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.