zoukankan      html  css  js  c++  java
  • 对数据库的操作

    liunx虚拟机连接本机MySQL:https://blog.csdn.net/qq_35151255/article/details/100046039

    对数据库的查询操作:

      private static String DriverName="com.mysql.jdbc.Driver";
      private static String UName="root";
      private static String Upwd="1108.shjzh..sql.lq";
      private static String Url="jdbc:mysql://localhost:3306/login";
      public boolean exit(int sno)   //根据学号查询学生是否存在 如果存在返回true,不存在返回false
      {
          return query(sno)==null?false:true;
      }
      public Student query(int sno)   //查询学生是否存在
      {
          Student student=null;
          Connection conn=null;
          PreparedStatement pstmt=null;
          ResultSet result=null;
          try {
              Class.forName(DriverName);
              conn=DriverManager.getConnection(Url, UName, Upwd);
              String sql="select * from student where sno=?";
              pstmt= conn.prepareStatement(sql);
              pstmt.setInt(1, sno);
              result=pstmt.executeQuery();
              if(result.next())
              {
                  int no=result.getInt("sno");
                  String name=result.getString("name");
                  int age=result.getInt("age");
                  String address=result.getString("address");
                  student=new Student(no, name, age, address);
              }
              return student;
          }
          catch(ClassNotFoundException e)
          {
              e.printStackTrace();
              return null;
          } 
          catch (SQLException e) 
          {
            e.printStackTrace();
            return null;
        }
          catch(Exception e)
          {
              e.printStackTrace();
              return null;
          }
          finally {
              try {
                  if(result!=null) result.close();
                  if(pstmt!=null) pstmt.close();
                  if(conn!=null) conn.close();
              }
              catch(SQLException e)
              {
                  e.printStackTrace();
              }
          }
      }
       private final static String DriverName="com.mysql.jdbc.Driver";
       private final static String Uname="root";
       private final static String Upwd="1108.shjzh..sql.lq";
       private final static String DbURL="jdbc:mysql://localhost:3306/login";
      public static int Connect(LoginBean loginbean)   //查询输入信息是否正确
      {
          int  flag=-1;   //flag=1(登录成功)
          Connection conn=null;
          PreparedStatement pstmt=null;
          ResultSet result=null;
          int count=-1;
          try {
              Class.forName(DriverName);
              conn=DriverManager.getConnection(DbURL, Uname, Upwd);
              String sql="select count(*) from user where name=? and pwd=?";
              pstmt=conn.prepareStatement(sql);
              pstmt.setString(1, loginbean.getName());
              pstmt.setString(2, loginbean.getPwd());
              result=pstmt.executeQuery();
              if(result.next())
              {
                  count=result.getInt(1);
              }
              if(count>0)
              {
                  return 1;    //登录成功
              }
              else
              {
                  return 0;  //登录失败(用户名,密码有误)
              }
          }
          catch(ClassNotFoundException  e)
          {
              e.printStackTrace();
              return -1;  
          }
          catch(SQLException e)
          {
              e.printStackTrace();
              return -1;
          }
          catch(Exception e)
          {
              e.printStackTrace();
              return -1;
          }
          finally
          {
              try 
              {
                  result.close();
                  pstmt.close();
                  conn.close();
              }
              catch(SQLException e)
              {
                  e.printStackTrace();
              }
              catch(Exception e)
              {
                  e.printStackTrace();
              }
          }
      }

    对数据库的添加操作:

     
    private static String DriverName="com.mysql.jdbc.Driver";
    private static String UName="root";
    private static String Upwd="1108.shjzh..sql.lq";
    private static String Url="jdbc:mysql://localhost:3306/login";

    public boolean add(Student student) //添加学生
      {
          Connection conn=null;
          PreparedStatement pstmt=null;
          try {
              Class.forName(DriverName);
              conn=DriverManager.getConnection(Url, UName, Upwd);
              String sql="insert into student values(?,?,?,?)";
              pstmt= conn.prepareStatement(sql);
              pstmt.setInt(1, student.getSno());
              pstmt.setString(2, student.getName());
              pstmt.setInt(3, student.getAge());
              pstmt.setString(4, student.getAddress());
              int connt=pstmt.executeUpdate();
              if(connt>0)  return true;
              else return false;
          }
          catch(ClassNotFoundException e)
          {
              e.printStackTrace();
              return false;
          } 
          catch (SQLException e) 
          {
            e.printStackTrace();
            return false;
        }
          catch(Exception e)
          {
              e.printStackTrace();
              return false;
          }
          finally {
              try {
                  if(pstmt!=null) pstmt.close();
                  if(conn!=null) conn.close();
              }
              catch(SQLException e)
              {
                  e.printStackTrace();
              }
          }
      }

       Add add=new Add();
       public boolean addStudent(Student student)
       {
          if(!add.exit(student.getSno()))
          {
            add.add(student);
            return true;
          }
          else
          {
              System.out.println("此人已存在!");
               return false;
          }
       }

     

     注意:当mysql数据乱码时将连接mysql的URL改为

    private static String Url="jdbc:mysql://localhost:3306/login?useUnicode=true&characterEncoding=UTF-8"
  • 相关阅读:
    .net软件反编译笔记
    Asp.Net 网站一键部署技术(上)
    C# TreeGridView 实现进程列表
    HeidiSQL Navicat
    Flask项目中邮箱模块的应用
    如何在已有的Flask项目添加数据表结构?
    把u盘刻录成系统盘
    Object '<Tbl_next_week_change_state at 0x7f2c78d849e8>' is already attached to session '4' (this is '6')
    OSError: [Errno 98] Address already in use
    Linux下设置和查看环境变量
  • 原文地址:https://www.cnblogs.com/lq13035130506/p/11688886.html
Copyright © 2011-2022 走看看