后台连接MySql,实现后台操作数据库增删改查

目的:通过java后台操作,改变数据库中的值,实现增删改查。
思路:首先在java中连接到数据库的表,然后写一个调用的Class,来实现操作。

连接数据库的Class:(例如表名叫做dnf)

package oracleJdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Dnf {
	
	//声明Connection对象
	Connection con;
	//驱动程序名
	String driver = "com.mysql.jdbc.Driver";
	//url指定要访问的数据库名wx
	String url = "jdbc:mysql://192.168.1.205:3306/wx";
	//MySQL配置时的用户名
	String user = "root";
	//MySQL配置时的密码
	String password = "root";
	//遍历查询结果集
	PreparedStatement ps;
	ResultSet rs;
	//创建一个数据库链接
	Connection connection = null;
	

public Connection getConnection(){
	try {
		Class.forName(driver);
		//1.getConnection()方法,连接MySQL数据库!!
		con = DriverManager.getConnection(url, user, password);
		System.out.println("正在链接...");
	} catch (ClassNotFoundException e) {
		//数据库驱动类异常处理
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
		//数据库连接失败异常处理
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return con;	
}

//添加方法
public void InSert(String id,String lv,String equipment,String shizhuang,String wuqi) throws SQLException{
	connection = getConnection();
	//预处理添加数据,其中有两个参数--"?"
	ps = con.prepareStatement("insert into dnf (id,lv,equipment,shizhuang,wuqi)"+"values(?,?,?,?,?)");
	ps.setString(1,id);
	ps.setString(2,lv);
	ps.setString(3,equipment);
	ps.setString(4,shizhuang);
	ps.setString(5,wuqi);
	
	//执行更新
	ps.executeUpdate();
	System.out.println("插入成功...");
	
	//更新之后显示数据库内容
	//2.创建statement类对象,用来执行SQL语句!!
	Statement statement = con.createStatement();
	String sql = "select * from dnf";
	//3.ResultSet类,用来存放获取的结果集!!
	ResultSet rs = statement.executeQuery(sql);
	
	while(rs.next()){
		id = rs.getString("id");
		lv = rs.getString("lv");
		equipment = rs.getString("equipment");
		shizhuang = rs.getString("shizhuang");
		wuqi = rs.getString("wuqi");
		
		System.out.println(id +"\t"+lv+"\t"+equipment+"\t"+shizhuang+"\t"+wuqi );
	}
	rs.close();
	con.close();
}

//修改方法
public void Update(String id,String lv) throws SQLException{
	connection = getConnection();
	
	ps = con.prepareStatement("update dnf set lv=? where id=?");
	ps.setString(1,lv);
	ps.setString(2,id);
	
	//执行修改
	ps.executeUpdate();
	System.out.println("修改成功...");
	
	//修改之后显示数据库的内容
	Statement statement = con.createStatement();
	String sql = "select * from dnf";
	ResultSet rs = statement.executeQuery(sql);
	
	while(rs.next()){
		id = rs.getString("id");
		lv = rs.getString("lv");
		
		System.out.println(id+"\t"+lv);
	}
	rs.close();
	con.close();
}

//删除方法
public void delect(String id) throws SQLException{
	connection = getConnection();
	
	ps = con.prepareStatement("delete from dnf where id=?");
	ps.setString(1,id);
	//执行删除
	ps.executeUpdate();
	System.out.println("删除成功...");
	
	//删除之后显示数据库内容
	Statement statement = con.createStatement();
	String sql = "select * from dnf";
	ResultSet rs = statement.executeQuery(sql);
	
	while(rs.next()){
		id = rs.getString("id");
		System.out.println(id);
	}
	rs.close();
	con.close();
}

//查询方法
public void SelectAll() throws SQLException{
	connection = getConnection();
	
	ps = con.prepareStatement("select * from dnf");
	//执行查询
	System.out.println("查询成功...");
	
	Statement statement = con.createStatement();
	String sql = "select * from dnf";
	ResultSet rs = statement.executeQuery(sql);
	
	while(rs.next()){
		String id = rs.getString("id");
		String lv = rs.getString("lv");
		String equipment = rs.getString("equipment");
		String shizhuang = rs.getString("shizhuang");
		String wuqi = rs.getString("wuqi");
		
		System.out.println(id+"\t"+lv+"\t"+equipment+"\t"+shizhuang+"\t"+wuqi);
	}
	rs.close();
	con.close();
	}
}

调用的Class:

package oracleJdbc;

import java.sql.SQLException;

public class DnfTest {
	public static void main(String[] args) throws SQLException{
		Dnf df = new Dnf();
		//添加方法(一添加是一行):df.InSert("jianhuang", "100", "qingjia", "niantao", "19");
		//修改方法:
		df.Update("jianhuang", "100");
		//删除方法(一删是一行):df.delect("??");
		//查询方法:df.SelectAll();
	}
}

发布了21 篇原创文章 · 获赞 7 · 访问量 623
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览