MyEclipse下MySQL的数据库增删改查操作详解MyEclipse 下的mySQL
建⽴数据库连接并且实现简单的数据库增查改删,内附实例
2017 版myEclipse
8.0.16 mySQL-connector
作业要求如下:
增删改查独⽴为⽅法,在main函数⾥调⽤,为了代码的美观,我都把⼀些固定的变量写在外⾯啦~
数据库:database8
数据表:newProduct
因为我的mySQL版本⽐较新,“sql.jdbc.Driver"变成"sql.cj.jdbc.Driver”
private static final String DRIVER ="sql.cj.jdbc.Driver";//更新
private static final String URL ="jdbc:mysql://localhost:3306/database8";
private static final String ACCOUNT ="root";
private static final String PSW ="12345";
//查询数据表全部数据,如下
public static void queryAll(){
try{
Class.forName(DRIVER);//载⼊JDBC驱动
Connection conn = Connection(URL, ACCOUNT,PSW);//建⽴数据库连接  Statement stmt =  ateStatement();//创建statement对象
String sql ="SELECT * wProduct";//查询语句
ResultSet rs = uteQuery(sql);//创建数据对象,执⾏sql
System.out.println("id"+"\t"+"pName"+"\t"+"origin"+"\t"+"price"+"\t"+"inventory");
()){
System.out.Int("id")+"\t");
System.out.String("pName")+"\t");
System.out.String("origin")+"\t");
System.out.Float("price")+"\t");
System.out.Int("inventory")+"\t");
System.out.println();
}
rs.close();
stmt.close();
conn.close();
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
}
/
/查询⼤于指定价格的所有产品,如下
public static void printAbovePrice(float price){
try{
Class.forName(DRIVER);//载⼊JDBC驱动
Connection conn = Connection(URL, ACCOUNT,PSW);//建⽴数据库连接  Statement stmt =  ateStatement();//创建statement对象
String sql1 ="SELECT * wProduct WHERE price>";//查询语句
Float Price =new Float(price);
String sql = sql1 +String();
ResultSet rs = uteQuery(sql);//创建数据对象,执⾏sql
System.out.println("id"+"\t"+"pName"+"\t"+"origin"+"\t"+"price"+"\t"+"inventory");
()){
System.out.Int("id")+"\t");
System.out.String("pName")+"\t");
System.out.String("origin")+"\t");
System.out.Float("price")+"\t");
System.out.Int("inventory")+"\t");
System.out.println();
}
rs.close();
stmt.close();
conn.close();
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
}
//增加指定数据,如下
public static void add(int id, String pName, String origin,float price,int invent){
try{
Class.forName(DRIVER);//载⼊JDBC驱动
Connection conn = Connection(URL, ACCOUNT,PSW);//建⽴数据库连接
Statement stmt =  ateStatement();//创建statement对象
String sql1 ="INSERT INTO newProduct VALUES(";
Integer Id =new Integer(id);
Integer Invent =new Integer(invent);
Float Price =new Float(price);
String sql = sql1 + Id.toString()+",'"+pName+"','"+origin+"',"+String()+","+String()+")";  uteUpdate(sql);//创建数据对象,执⾏sql
System.out.println("Success in insert!");
stmt.close();
conn.close();
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
}
删除指定id,如下
public static void delete(int id){
try{
Class.forName(DRIVER);//载⼊JDBC驱动
Connection conn = Connection(URL, ACCOUNT,PSW);//建⽴数据库连接
Statement stmt =  ateStatement();//创建statement对象
String sql1 ="DELETE FROM newProduct WHERE id=";
Integer Id =new Integer(id);
String sql = String();//删除id语句
System.out.println("Success in delete!");
stmt.close();
conn.close();
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
}
/
/修改指定数据的指定库存,如下
public static void update(int id,int invent){
try{
Class.forName(DRIVER);//载⼊JDBC驱动
Connection conn = Connection(URL, ACCOUNT,PSW);//建⽴数据库连接
Statement stmt =  ateStatement();//创建statement对象
String sql1 ="UPDATE newProduct SET inventory =inventory-";
String sql2 =" WHERE id=";
Integer Id =new Integer(id);
Integer Invent =new Integer(invent);
String sql = sql1 +String()+ sql2+ Id.toString();//更新id=4语句
System.out.println("Success in update!");
stmt.close();
conn.close();
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
}
最后的最后,当然是⼀个main函数调⽤啦~
public static void main (String args[]){
Product p =new Product();
//p.delete(5);
//p.add(6,"iTouch","China",399,5000);
//p.printAbovePrice(4000);
p.update(2,500);
//p.queryAll();
}
终端截图,如下
我是利⽤字符串的拼接完成mySQL的操作滴,在拼接的时候需要注意:
1. 增加完整数据时,正常插⼊应该是类似这样:INSERT INTO product VALUES(2,‘三星显⽰器’,‘电脑组件’,‘杭
州’,870); 所以在拼接时不要忘记逗号左右两边的单引号
我也有⼀个问题:
基本的增删改查语句我的每个⽅法⾥⾯都重新建⽴连接,这有必要吗?有没有什么可以省略的部分?欢迎⼤家指导哦~

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。