• Java连接本地MySQL数据库进行增删改查操作


    package Dao;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import entity.UserInfo;
    
    public class JDBC {
        Connection conn;
        PreparedStatement ps;
        ResultSet rs;
        /**
         * 写一个连接数据库的方法
         */
        public Connection getConnection(){
            String url="jdbc:mysql://localhost:端口/数据库";
            String userName="root";
            String password="密码";
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                System.out.println("找不到驱动!");
                e.printStackTrace();
            }
            try {
                conn=DriverManager.getConnection(url, userName, password);
                if(conn!=null){
                    System.out.println("connection successful");
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                    System.out.println( "connection fail");
                e.printStackTrace();
            }
            return conn;
        }
        /**
         * 写一个查询数据库语句的方法
         */
        public void QuerySql(){
            //1、执行静态SQL语句。通常通过Statement实例实现。   
            // 2、执行动态SQL语句。通常通过PreparedStatement实例实现。   
            // 3、执行数据库存储过程。通常通过CallableStatement实例实现。
            System.out.println("query");
            UserInfo u;
    //        j.Connection();
            String sql="select * from userInfo";
            try {
                conn=getConnection();//连接数据库
                ps=conn.prepareStatement(sql);// 2.创建Satement并设置参数
                rs=ps.executeQuery(sql);  // 3.ִ执行SQL语句
                // 4.处理结果集
                while(rs.next()){
                    u=new UserInfo();
                    u.setUserId(rs.getInt("userId"));
                    u.setUserName(rs.getString("userName"));
                    u.setPassword(rs.getString("password"));
                    u.setRemark(rs.getString("remark"));
                    System.out.println("uesrName"+u.getUserName());
                }
                System.out.println(rs.next());
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                //释放资源
                try {
                    rs.close();
                    ps.close();
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        @SuppressWarnings("null")
        public int add(UserInfo user){
            UserInfo u =new UserInfo();
            int row=0;
    //        j.Connection();
            String sql="insert into userInfo(userName,password) values(?,?)";
            try {
                conn=getConnection();//连接数据库
                ps=conn.prepareStatement(sql);// 2.创建Satement并设置参数
    //            rs=ps.executeQuery();  // 3.ִ执行SQL语句,緊緊用于查找語句
                //sql語句中寫了幾個字段,下面就必須要有幾個字段
                 ps.setString(1, user.getUserName());
                ps.setString(2, user.getPassword());
                // 4.处理结果集
                row=ps.executeUpdate();
                System.out.println(row+user.getUserName()+user.getPassword());
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                try {
                    ps.close();
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
            return row;
        }
        /**
         * @return修改数据
         */
        public int update(UserInfo user){
            UserInfo u;
            int row=0;
    //        j.Connection();
            String sql="update userInfo set userName=?,password=? where userId=?";
            try {
                conn=getConnection();//连接数据库
                ps=conn.prepareStatement(sql);// 2.创建Satement并设置参数
    //            rs=ps.executeQuery(sql);  // 3.ִ执行SQL语句,緊緊用于查找語句
                //sql語句中寫了幾個字段,下面就必須要有幾個字段
                ps.setString(1, user.getUserName());
                ps.setString(2, user.getPassword());
                ps.setInt(3, user.getUserId());
                // 4.处理结果集
                row=ps.executeUpdate();
                System.out.println(row);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                //释放资源
                try {
    //                rs.close();
                    ps.close();
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return row;
        }
        /**
         * @return删除操作
         */
        public int delete(UserInfo user){
            UserInfo u;
            int row=0;
    //        j.Connection();
            String sql="delete from userInfo where userId=?";
            try {
                conn=getConnection();//连接数据库
                ps=conn.prepareStatement(sql);// 2.创建Satement并设置参数
    //            rs=ps.executeQuery(sql);  // 3.ִ执行SQL语句,緊緊用于查找語句
                //sql語句中寫了幾個字段,下面就必須要有幾個字段
                ps.setInt(1, user.getUserId());
                // 4.处理结果集
                row=ps.executeUpdate();
                System.out.println(row);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                //释放资源【執行完sql要記得釋放資源】
                try {
    //                rs.close();
                    ps.close();
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return row;
        }
        public static void main(String[] args) {
            JDBC j=new JDBC();
    //        j.getConnection();
            j.QuerySql();//在控制台顯示出查找方法
    //        UserInfo u=new UserInfo();
    //        u.setUserId(5);
    //        u.setUserName("cool");
    //        u.setPassword("123abc");
    //        j.update(u);////在控制台顯示出修改方法
        }
    }

    from: http://blog.csdn.net/qianquan003/article/details/23364381?utm_source=tuicool&utm_medium=referral

  • 相关阅读:
    QTP的那些事不能识别web上的测试对象
    ASP.NET 2.0 页面事件执行顺序 摘自http://www.cnblogs.com/chinadragon/archive/2009/11/21/1607761.html
    javascript replace方法的使用注意点
    HTML转义字符转载http://www.8189090.com/character/
    有无Global.asax文件对Session的影响
    四叉树索引引用自http://hi.baidu.com/geochenyj/blog/item/189f2fed07d041d6b31cb1b6.html
    GirdView固定行头
    CSS的常用滤镜(filter)属性及语句大全摘自http://www.8tops.com/113_skill_8267CEB6473B4AF1ABF669340E3AD2EF.htm
    Clone基类http://www.legalsoft.com.cn/docs/986.html
    自定义控件不能设置属性
  • 原文地址:https://www.cnblogs.com/GarfieldEr007/p/5746134.html
Copyright © 2020-2023  润新知