爱问知识人 爱问教育 医院库

帖子有图片如何在数据库建表

首页

帖子有图片如何在数据库建表

Dao中方法怎么实现》。。。

提交回答

全部答案

    2018-05-13 04:32:02
  •   具体点,用框架还是javabean?
    用javabean:
    创建db类:
    import java。sql。SQLException;
    public class DbConn {

    /**
    * 获取数据库连接对象
    * @return conn
    */
    public static Connection getConn()
    {
    Connection conn=null;
    /*
    * 连接的用户名
    */
    String user="guo";
    /*
    *密码
    */
    String password="guo";
    /*
    * oracle的连接字符串url:写法固定
    * jdbc:oracle:thin:@连接到的主机(ip地址或主机名):oracle端口号:数据库名;
    */
    String url="jdbc:oracle:thin:@localhost:1521:inspur";
    try {
    /*
    * 加载oracle驱动类到内存
    */
    Class。
      forName("oracle。jdbc。driver。OracleDriver");
    /*
    *通过驱动管理类DriverManager获取驱动对象
    */
    conn=DriverManager。
      getConnection(url,user,password);
    if(conn!=null)
    {
    System。out。println("数据库连接成功!");
    }
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e。
      printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e。printStackTrace();
    }
    return conn;
    }
    public static void main(String[] args) {
    DbConn。
      getConn();
    }
    }
    dao层调用db类:
    package com。inspur。dao;
    import java。sql。Connection;
    import java。
      sql。PreparedStatement;
    import java。sql。ResultSet;
    import java。sql。SQLException;
    import javax。
      xml。registry。infomodel。User;
    import com。inspur。db。DbConn;
    import com。inspur。pojo。Users;
    /**
    * 此类是操作t_users表中数据的dao类
    * @author guojl
    *
    */
    public class UsersDao {
    /*
    * 声明此类中所有方法共用的对象
    */
    PreparedStatement pstmt=null;
    ResultSet rs=null;
    Connection conn=null;

    /**
    * 根据login。
      jsp页面输入的用户名和密码,判断用户是否存在
    * @param uname
    * @param pass
    * @return bool: 用户名和密码正确返回true,否则false
    */
    public boolean checkUserLogin(String userId,String pass)
    {
    boolean bool=false;
    //1。
      获取连接
    conn=DbConn。getConn();
    //2。创建sql
    String sql="select userid from t_users where userid=? and userpass=?"; //所有参数的地方用占位符?表示
    try {
    //3。
      给占位符赋值
    pstmt=conn。prepareStatement(sql);
    pstmt。setString(1, userId);
    pstmt。setString(2, pass);

    //4。
      发送执行sql语句
    rs=pstmt。executeQuery();

    //5。判断结果集中数据
    while(rs。next())
    {
    //如果结果集中有数据,rs。
      next()方法,返回true
    bool=true;
    }

    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e。
      printStackTrace();
    }finally{
    //释放资源
    try {
    if(rs!=null)
    {
    rs。close();
    }
    if(pstmt!=null)
    {
    pstmt。
      close();
    }
    if(conn!=null)
    {
    conn。close();
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e。
      printStackTrace();
    }
    }
    return bool;
    }
    /**
    * 实现将新用户添加到t_users表中
    * @param user:从注册页面获取user对象
    * @return: 添加成功,返回true,否则返回false
    */
    public boolean addUsers(Users user)
    {
    boolean bool=false;
    //1。
      初始化连接
    conn=DbConn。getConn();
    //2。创建sql语句
    String sql="insert into t_users values(?,?,?,?)";
    //3。
      给占位符赋值
    try {
    pstmt=conn。prepareStatement(sql);
    pstmt。setString(1, user。getUserId());
    pstmt。
      setString(2, user。getUserName());
    pstmt。setString(3, user。getPass());
    pstmt。setString(4, user。getSex());
    //4。
      发送执行sql语句
    int t=pstmt。executeUpdate();
    if(t>0)
    {
    //t大于0,表示插入语句影响了表中至少1行,语句执行成功
    bool=true;
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e。
      printStackTrace();
    }finally{
    //释放资源
    try {
    if(pstmt!=null)
    {
    pstmt。
      close();
    }
    if(conn!=null)
    {
    conn。close();
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e。
      printStackTrace();
    }
    }
    return bool;
    }
    }
    还有个修改的功能
    如果解决了你的问题,请点击采纳啊!。
      

    李***

    2018-05-13 04:32:02

类似问题

换一换

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 171-190
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):