zoukankan      html  css  js  c++  java
  • JDBC通用更新数据

    JDBC_test

    public class Jdbc_test {
    
        public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, NoSuchMethodException, InvocationTargetException, IOException {
            String sql = "insert into emp values(default,?,?)"; //预编译语句
            //更新语句
            update(sql,"admin3",111);
        }
    
        public static void update(String sql,Object...args) throws SQLException, IOException, ClassNotFoundException {
            //加载配置文件
            Connection conn = JDBCUtils.getConnection();
            //插入语句
            PreparedStatement ps =conn.prepareStatement(sql);
            for(int i=0;i<args.length;i++){
                ps.setObject(i+1,args[i]);
            }
            ps.executeUpdate(); //执行
            JDBCUtils.close(conn,ps);//关闭资源
        }
    }

    JDBCUtils.Class

    public class JDBCUtils {
        /**
         * 建立连接
         * @return Connection
         * @throws IOException
         * @throws ClassNotFoundException
         * @throws SQLException
         */
        public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
            InputStream is = Jdbc_test.class.getClassLoader().getResourceAsStream("jdbc.propertise");
            Properties properties = new Properties();
            properties.load(is);
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String url = properties.getProperty("url");
            String driver = properties.getProperty("driver");
            //连接数据库
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url,user,password);
            return conn;
        }
    
        /**
         * 关闭资源
         * @param conn
         * @param sm
         * @throws SQLException
         */
        public static void close(Connection conn, Statement sm) throws SQLException {
            if (conn != null){
                conn.close();
            }
            if(sm != null){
                sm.close();
            }
    
        }
    }
  • 相关阅读:
    升级WP应用时注意的问题——WMAppManifest.xml
    MVVM Light (Part 4)
    Windows Phone 7的About模板——Your Last About Dialog(2)支持多语言
    MVVM Light 开始
    在ScheduledTaskAgent中使用HttpWebRequest
    年会抽奖程序 支持单次单个抽奖和单次多个抽奖,自定义抽奖设置
    WIndows Phone 7的MVVM Light框架
    MVVM Light (Part 3)
    MVVM Light 行为
    [转]如何在设计中应用颜色搭配技巧
  • 原文地址:https://www.cnblogs.com/baisha/p/15467296.html
Copyright © 2011-2022 走看看