JAVA操作数据库

时间:2022-09-12 15:08:58
package com.dg.text;
import java.sql.*; import java.io.*; import com.dg.dao.*;
public class dbtext { public static void main(String args[]){ DBConnection  conn = new DBConnection(); //查询数据库语句 String szsql = "select * from test"; ResultSet rs =conn.executeQuery(szsql); try{ while(rs.next()){ String rt = String.valueOf(rs.getInt("id")); String name = rs.getString("name"); System.out.print(rt); System.out.print("    "); System.out.println(name); } } catch(Exception e){ e.printStackTrace(); } //操作数据库语句---insert try{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = br.readLine(); String insertSql = "insert into test(name) values('"+input+"')"; boolean tf = conn.execute(insertSql); if(tf){ System.out.println(tf); } else{ System.out.println("插入数据失败!"); } } catch(Exception e){ e.printStackTrace(); } //操作数据库语句---update try{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input = br.readLine(); String updateSql = "update test set name='"+input+"1' where name='"+input+"'"; boolean tf = conn.execute(updateSql); if(tf){ System.out.println(tf); } else{ System.out.println("插入数据失败!"); } } catch(Exception e){ e.printStackTrace(); } //操作数据库语句---delete try{ String updateSql = "delete from test where name='1'"; boolean tf = conn.execute(updateSql); if(tf){ System.out.println(tf); } else{ System.out.println("插入数据失败!"); } } catch(Exception e){ e.printStackTrace(); } } }