java实现增删改查代码

public static void main(String [] args) throws Exception
		{
			System.out.print("hello");
			text t = new text();
			
			//第一种插入数据
			t.addStudebt1();

		
		}
			 
		
					

		
//增加入数据
	public void addStudebt1() throws Exception{
			
		String sql = "insert into student values (11,'root', '123','nan','100')";		
		String url = "jdbc:mysql://127.0.0.1:3306/text?serverTimezone=GMT%2B8";
		Class.forName("com.mysql.cj.jdbc.Driver");
		Connection con = DriverManager.getConnection(url,"root","mysql");
		PreparedStatement pre = con.prepareStatement(sql);
		pre.execute();
		pre.close();
			
			
		}
		
		public void addStudebt(int id,String userName,String password, String sex,double string) throws Exception{
			
			String sql = "insert into student values(?,?,?,?,?)";		
			
			String url = "jdbc:mysql://127.0.0.1:3306/text?serverTimezone=GMT%2B8";
			Class.forName("com.mysql.cj.jdbc.Driver");
			Connection con = DriverManager.getConnection(url,"root","mysql");
			PreparedStatement pre = con.prepareStatement(sql);
			pre.setInt(1, id);
			pre.setString(2, userName);	
			pre.setString(3, password);	
			pre.setString(4, sex);
			pre.setDouble(5, string);	
			
			pre.execute();
			pre.close();
			
			
		}
		
//删除数据
		public void removeStudebt(int id) throws Exception{
				
			String sql = "delete from student where id=?";
			
			String url = "jdbc:mysql://127.0.0.1:3306/text?serverTimezone=GMT%2B8";
			Class.forName("com.mysql.cj.jdbc.Driver");
			Connection con = DriverManager.getConnection(url,"root","mysql");
			
		
			PreparedStatement pre = con.prepareStatement(sql);
			pre.setInt(1, id);
			
			pre.execute();
			pre.close();
			
			
		}
//更新数据
		public void updateStudebt(String userName,int id) throws Exception{
			
			String sql = "update student set user_name=? where id=?";
			
			String url = "jdbc:mysql://127.0.0.1:3306/text";
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection(url,"root","123456");
			PreparedStatement pre = con.prepareStatement(sql);
			pre.setInt(1, id);
			pre.setString(2, userName);		
			pre.execute();
			pre.close();
			
			
		}
		
	import java.sql.Connection;
	import java.sql.DriverManager;
	import java.sql.PreparedStatement;
	import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class text {	
public static void main(String [] args) throws Exception
		{
			System.out.print("hello");
			text t = new text();
			
	
			List<student>list= t.queryStudebt();
			System.out.print(list);
			
			
			
		
		}
		public List<student> queryStudentPage(Map<String ,Integer> map) throws Exception
		{
			String sql = "select * from student limit ?,?";
			String url = "jdbc:mysql://127.0.0.1:3306/text";
			Class.forName("com.mysql.jdbc.Driver");
			
			
			Connection con = DriverManager.getConnection(url,"root","123456");
			PreparedStatement pre = con.prepareStatement(sql);
			
			int start =(map.get("pageStart")-1)*map.get("pageSize");
			pre.setInt(1, start);
			pre.setInt(2, map.get("pageSize"));  
			
			List<student> list = new ArrayList<>();
			 ResultSet res =pre.executeQuery();
			 while(res.next()){
					int id= res.getInt(1);
					 String userName=res.getString(2);
					String passwprd= res.getString(3);
					 String sex =res.getString(4);
					double score= res.getDouble(5);
			 }
			return list;
				 
			
			
		}
		
		
		public List<student> queryStudebt() throws Exception{
			
			String sql = "select * from student";		
		
//			String url = "jdbc:mysql://127.0.0.1:3306/text?serverTimezone=GMT%2B8";
//			Class.forName("com.mysql.cj.jdbc.Driver");
//			Connection con = DriverManager.getConnection(url,"root","mysql");
			
			Connection con = ConnectionUtils.getCon();
			PreparedStatement pre = con.prepareStatement(sql);
			 ResultSet res =pre.executeQuery();
			 List<student> list = new ArrayList<>();
			 while(res.next()){
				int id= res.getInt(1);
				 String userName=res.getString(2);
				String passWord= res.getString(3);
				 String sex =res.getString(4);
				double score= res.getDouble(5);
				student s= new student (id,userName,passWord,sex,score);
				list.add(s);
			 }
		

	
			pre.close();
			return list;
			
			
		}
		
}
package com.util;

public class Student {
private Integer id;
private String userName;
private String sex;
private String passWord;
private Double score;
public Integer getId() {
	return id;
}
public void setId(Integer id) {
	this.id = id;
}
public String getUserName() {
	return userName;
}
public void setUserName(String userName) {
	this.userName = userName;
}
public String getSex() {
	return sex;
}
public void setSex(String sex) {
	this.sex = sex;
}
public String getPassWord() {
	return passWord;
}
public void setPassWord(String passWord) {
	this.passWord = passWord;
}
public Double getScore() {
	return score;
}
public void setScore(Double score) {
	this.score = score;
}
public Student(Integer id, String userName, String sex, String passWord,
		Double score) {
	super();
	this.id = id;
	this.userName = userName;
	this.sex = sex;
	this.passWord = passWord;
	this.score = score;
}
@Override
public String toString() {
	return "Student [id=" + id + ", userName=" + userName + ", sex=" + sex
			+ ", passWord=" + passWord + ", score=" + score + "]";
}
public Student() {
	super();
}

}
package com.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class TestJdbc {
	// List Set Map
	// API String Math Date File Calendar

	//
	
	public static void main(String[] args) throws Exception {
		TestJdbc t = new TestJdbc();
		List<Student> list=t.queryStudent();
		System.out.println(list);
	}
	
	public List<Student> queryStudent() throws Exception{
		String sql="select * from student";
	
		PreparedStatement pre = con.prepareStatement(sql);
		ResultSet res=pre.executeQuery();// ִ��sql���
		List<Student> list=new ArrayList<>();
		while(res.next()){
			int id=res.getInt(1);
			String userName=res.getString(2);
			String sex=res.getString(3);
			String passWord=res.getString(4);
			double score=res.getDouble(5);
			Student s=new Student(id, userName, sex, passWord, score);
			list.add(s);
		}
		con.close();
		return list;
	}

	public void saveStudent(int id, String userName, String sex,
		String passWord, double score) throws Exception {
		String sql = "insert into student values (?,?,?,?,?)";
		Connection con = ConnectionUtils.getCon();// �������ݿ�
		PreparedStatement pre = con.prepareStatement(sql);
		pre.setInt(1, id);
		pre.setString(2, userName);
		pre.setString(3, sex);
		pre.setString(4, passWord);
		pre.setDouble(5, score);
		pre.execute();// ִ��sql���
		pre.close();
	}

	public void updateStudent(String userName, int id) throws Exception {
		String sql = "update student set user_name=? where id=?";
		Connection con = ConnectionUtils.getCon();// �������ݿ�
		PreparedStatement pre = con.prepareStatement(sql);
		pre.setString(1, userName);
		pre.setInt(2, id);
		pre.execute();
		pre.close();
	}

	public void removeStudent(int id) throws Exception {
		String sql = "delete from student where id=?";
		Connection con = ConnectionUtils.getCon();// �������ݿ�
		PreparedStatement pre = con.prepareStatement(sql);
		pre.setInt(1, id);
		pre.execute();
		pre.close();
	}
}
package com.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

public class ConnectionUtils {
public static Connection getCon() throws Exception{
	Properties pro=new Properties();
	InputStream is=ConnectionUtils.class.getResourceAsStream("jdbc.properties");
	pro.load(is);//��is�ֽ������ص�pro������
	String url=pro.getProperty("url");
	String userName=pro.getProperty("userName");
	String passWord=pro.getProperty("passWord");
	String driver=pro.getProperty("driver");
	Class.forName(driver);
	Connection con = DriverManager.getConnection(url, userName, passWord);// �������ݿ�
	return con;
}
}

  • 3
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值