java~數據庫的增刪查修

數據庫的建立:

Student表,建立三個字段:
1. id(主鍵自增)
2. name
3. tel

代碼實現:

package cn.edu.hpu.mode;

public class Student {
	private int id;
	private String name;
	private String tel;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getTel() {
		return tel;
	}
	public void setTel(String tel) {
		this.tel = tel;
	}
	
	
}
數據庫數據的增加代碼:

package cn.edu.hpu.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Add {
	public static void main(String[] args) throws Exception {
		Class.forName("com.mysql.jdbc.Driver");
		Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_student", "root", "root");
		if(con!=null){
			System.out.println("數據庫連接成功!");
			String sql = "insert into student(name,tel) values(?,?)";
			PreparedStatement pst =con.prepareStatement(sql);
			pst.setString(1, "張怡");
			pst.setString(2, "5907");
			int rows = pst.executeUpdate();//受影響的行數
			if (rows > 0) {
				System.out.println("插入成功!");
			}else{
				System.out.println("插入失敗!");
			}
		}else {
			System.out.println("數據庫連接失敗!");
		}
	}
}

刪除數據的代碼實現:

package cn.edu.hpu.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;



public class Del {
	public static void main(String[] args) throws Exception {
		Class.forName("com.mysql.jdbc.Driver");
		Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_student", "root", "root");
		String sql="delete from student where id=?";
		PreparedStatement pst = con.prepareStatement(sql);
		pst.setInt(1,5);
		
		int row=pst.executeUpdate();
		if (row>0) {
			System.out.println("刪除成功!");
		}else {
			System.out.println("刪除失敗!");
		}
	}
}
查找數據庫數據的代碼實現:

package cn.edu.hpu.service;

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

import cn.edu.hpu.mode.Student;


public class Get {
	public static void main(String[] args) throws Exception {
		Student s = new Student();
		int id = 8;
		Class.forName("com.mysql.jdbc.Driver");
		Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_student", "root", "root");
		String sql="select * from student where id="+id;
		
		List<Student> list = new ArrayList<Student>();

		Statement st = null;
		ResultSet rs = null;
		
	
		st = con.createStatement();
		rs = st.executeQuery(sql);
		while (rs.next()) {
			Student stu = new Student();
			stu.setId(rs.getInt("id"));
			stu.setName(rs.getString("name"));
			stu.setTel(rs.getString("tel"));

			list.add(stu);
		}
		System.out.println(list);
	}
}

修改數據的代碼實現:

package cn.edu.hpu.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Update {
	public static void main(String[] args) throws Exception {
		Class.forName("com.mysql.jdbc.Driver");
		Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_student", "root", "root");
		
		String sql="update student set name=?,tel=? where id=?";
		PreparedStatement pst = con.prepareStatement(sql) ;
		pst.setString(1, "王猛");
		pst.setString(2, "2069");
		pst.setInt(3, 8);
		
		int row=pst.executeUpdate();
		if(row>0){
			System.out.println("更新成功!");
		}else {
			System.out.println("更新失敗!");
		}
		}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章