温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java数据库连接、查询、更新等

发布时间:2020-10-11 04:56:46 来源:脚本之家 阅读:97 作者:璐潇晓璐 栏目:编程语言

1、java数据库连接、查询、更新

nameGet='%'+nameGet+'%'; 
String sqlGname = "SELECT * FROM GOODS WHERE GNAME LIKE ?"; 
try 
{ 
  pstmt = conn.prepareStatement(sqlGname); 
  pstmt.setString(1, nameGet); 
  rs = pstmt.executeQuery(); 
  while (rs.next()) 
  { 
    int gid = rs.getInt("gid"); 
    String gname = rs.getString(2); 
    double gprice = rs.getDouble(3); 
    int gnum = rs.getInt(4); 
    Goods goods = new Goods(gid,gname,gprice,gnum); 
    goodsList.add(goods); 
    } 
  } catch (SQLException e) 
  { 
    e.printStackTrace(); 
  }finally 
  { 
    DbClose.queryClose(pstmt, rs, conn); 
  } 

2、连接数据库

public final class DbConn 
{ 
  public static Connection getconn() 
  { 
    Connection conn = null; 
     
    String user  = "root"; 
    String passwd = "root"; 
    String url = "jdbc:mysql://localhost:3306/shop"; 
     
    //已加载完驱动 
    try 
    { 
      Class.forName("com.mysql.jdbc.Driver");  
      conn = DriverManager.getConnection(url,user,passwd); 
    }catch (SQLException e) 
    { 
      e.printStackTrace(); 
    } 
    catch (ClassNotFoundException e) 
    { 
      e.printStackTrace(); 
    } 
    return conn; 
  } 
 
} 

这篇文章就介绍到这,下一篇将为大家更好的更相关的文章。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI