zoukankan      html  css  js  c++  java
  • WEB(JSP)下的JDBC操作

    package com.zss.www;


    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    import com.mysql.jdbc.Connection;
    import com.mysql.jdbc.PreparedStatement;


    public class DBconn {


    private Connection conn=null;
    private PreparedStatement stmt=null;
    private ResultSet rs=null;

    public DBconn(){


    try {
    String driver="com.mysql.jdbc.Driver";
    String url="jdbc:mysql://localhost:3306/school";
    String user="root";
    String password="711109";
    Class.forName(driver);
    conn = (Connection) DriverManager.getConnection(url,user,password);
    System.out.println("-------连接成功------");
    } catch(ClassNotFoundException classnotfoundexception) {
    classnotfoundexception.printStackTrace();
    System.err.println("db: " + classnotfoundexception.getMessage());
    } catch(SQLException sqlexception) {
    System.err.println("db.getconn(): " + sqlexception.getMessage());
    }







    }


    public void DBConn(String driver,String url,String user,String password){

    try {
    //driver="com.mysql.jdbc.Driver";
    //url="jdbc:mysql://localhost:3306/school";
    //user="root";
    //password="711109";
    Class.forName(driver);
    conn = (Connection) DriverManager.getConnection(url,user,password);
    System.out.println("-------连接成功------");
    } catch(ClassNotFoundException classnotfoundexception) {
    classnotfoundexception.printStackTrace();
    System.err.println("db: " + classnotfoundexception.getMessage());
    } catch(SQLException sqlexception) {
    System.err.println("db.getconn(): " + sqlexception.getMessage());
    }

    }

    public void doInsert(String sql) {
    try {
    stmt = (PreparedStatement)conn.prepareStatement(sql);
    int i = stmt.executeUpdate(sql);
    } catch(SQLException sqlexception) {
    System.err.println("db.executeInset:" + sqlexception.getMessage());
    }finally{

    }
    }

    public void doDelete(String sql) {
    try {
    stmt = (PreparedStatement)conn.prepareStatement(sql);
    int i = stmt.executeUpdate(sql);
    } catch(SQLException sqlexception) {
    System.err.println("db.executeDelete:" + sqlexception.getMessage());
    }
    }

    public void doUpdate(String sql) {
    try {
    stmt = (PreparedStatement)conn.prepareStatement(sql);
    int i = stmt.executeUpdate(sql);
    } catch(SQLException sqlexception) {
    System.err.println("db.executeUpdate:" + sqlexception.getMessage());
    }
    }

    public ResultSet doSelect(String sql) {
    try {
    stmt = (PreparedStatement)conn.prepareStatement(sql);
    rs = stmt.executeQuery(sql);
    System.out.println("取得结果集");
    } catch(SQLException sqlexception) {
    System.err.println("db.executeQuery: " + sqlexception.getMessage());
    }
    return rs;
    }

    public void close(ResultSet rs) throws SQLException, Exception {

    if (rs != null) {
    rs.close();
    rs = null;
    }

    if (stmt != null) {
    stmt.close();
    stmt = null;
    }

    if (conn != null) {
    conn.close();
    conn = null;
    }
    }

    public void close() throws SQLException, Exception {
    if (stmt != null) {
    stmt.close();
    stmt = null;
    }

    if (conn != null) {
    conn.close();
    conn = null;
    }
    }

    public static void main(String []args) throws Exception {
    DBconn db=new DBconn();
    ResultSet rs=db.doSelect("select * from dbuser where userNAME='chenjirong'");
    try {
    while(rs.next()) {
    System.out.println(rs.getString(1));
    System.out.println(rs.getString(2));
    System.out.println(rs.getString(3));
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    rs.close();
    db.close();
    }
    }

  • 相关阅读:
    python实用库:PrettyTable 学习
    centos启动错误:Inodes that were part of a corrupted orphan linked list found.
    C++:in namespace 'std' does not name a template type
    小程序实现单词查询搜索及搜索的历史记录
    小程序图片懒加载较完美解决方案
    下载文件到本地解压压缩包出现文件损坏,报错问题已解决
    彻底理解cookie,session,token
    vue全家桶(Vue+Vue-router+Vuex+axios)(Vue+webpack项目实战系列之二)
    与关系型数据库相比,MongoDB的优缺点
    漫谈JS 的继承方式
  • 原文地址:https://www.cnblogs.com/bgd150809222/p/6642402.html
Copyright © 2011-2022 走看看