欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-09 15:08:21
1、java数据库连接、查询、更新 nameget='%'+nameget+'%'; string sqlgname = "select * from goo...

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; 
  } 
 
} 

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