Java学习笔记——JDBC入门案例

JDBC入门案例

步骤

  1. 加载数据库驱动
  2. 建立数据库链接
  3. 创建preparedStatement
  4. 处理数据
  5. 关闭数据

入门案例

准备数据库

#新建表
CREATE TABLE student ( 
	sid INT(10) PRIMARY KEY,
	sname VARCHAR(50),
	age INT(10),
	gender VARCHAR(10)
);

#插入五条数据
INSERT INTO student (sid,sname,age,gender) VALUES (1,'asd',17,'male');
INSERT INTO student (sid,sname,age,gender) VALUES (2,'fgh',18,'female');
INSERT INTO student (sid,sname,age,gender) VALUES (3,'jkl',21,'male');
INSERT INTO student (sid,sname,age,gender) VALUES (4,'qwe',20,'female');
INSERT INTO student (sid,sname,age,gender) VALUES (5,'yui',19,'male');

编写Java程序

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
// 导入的包都为java.sql的包,
public class test_jdbc {

	public static void main(String[] args) {
		test_jdbc test = new test_jdbc();
		//在这里调用对象的方法来运行就可以了
		test.deleteStudent();
	}
	//查询所有的学生
	public void selectStudent() {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			//1.加载数据库驱动
			Class.forName("com.mysql.jdbc.Driver");
			//2.建立数据库的连接
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest","root","x5");
			//3.创建statement
			statement = connection.createStatement();
			//statement
			String sql = "select * from student";
			resultSet = statement.executeQuery(sql);
			//处理数据
			 while (resultSet.next()) {
				 
				int sid = resultSet.getInt("sid");
				String sname = resultSet.getString("sname");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				
				//将查询的数据写入对象
				Student student = new Student();
				student.setSid(sid);
				student.setSname(sname);
				student.setAge(age);
				student.setGender(gender);
				System.out.println(student);
			}
		} catch (Exception e) {
			System.out.println(e.toString());
		} finally {
			//不为空就关闭
			if(resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				resultSet = null;
			}
			
			//不为空就关闭
			if(statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				statement = null;
			}
			
			//不为空就关闭
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				connection = null;
			}
			System.out.println("==========查询成功==========");
		}
	}
		
	public void addStudent() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest","root","x5");
			// ?是占位符,会在后面写入
			String sql = "insert into student values (?,?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			//下面的1,2,3,4分别代表着上面sql语句中的第几个占位符
			preparedStatement.setInt(1, 1010);
			preparedStatement.setString(2, "hjh");
			preparedStatement.setInt(3, 22);
			preparedStatement.setString(4, "male");
			//执行sql语句
			preparedStatement.executeUpdate();
		} catch (Exception e) {
			System.out.println(e.toString());
		} finally {
		
			//不为空就关闭
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				connection = null;
			}
			
			//不为空就关闭
			if(preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				preparedStatement = null;
			}
			System.out.println("==========添加成功==========");
		}
	}
	
	public void deleteStudent() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://localhost/jdbctest", "root", "x5");
			//从学生表中删除学号为?的学生
			String sql = "delete from student where sid = ?";
			preparedStatement = connection.prepareStatement(sql);
			//写入1010,删除学号为1010的学生
			preparedStatement.setInt(1, 1010);
			//执行sql语句
			preparedStatement.executeUpdate();
			
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
		
			//不为空就关闭
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				connection = null;
			}
			
			//不为空就关闭
			if(preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				preparedStatement = null;
			}
			System.out.println("==========删除成功==========");
		}
	}
	
	public void updateStudent() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest","root","x5");
			//把学生表中sid为?的学生性别更改为?
			String sql = "update student set gender = ? where sid = ?";
			preparedStatement = connection.prepareStatement(sql);
			//把学生表中sid为1010 的学生性别更改为female(女)
			preparedStatement.setString(1, "female");
			preparedStatement.setInt(2, 1010);
			//执行sql语句
			preparedStatement.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
		
			//不为空就关闭
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				connection = null;
			}
			
			//不为空就关闭
			if(preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				preparedStatement = null;
			}
			System.out.println("==========修改成功==========");

		}
	}
	
	public void findStudent() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest","root","x5");
			//查询学生表中学生姓名为?的学生
			String sql = "select * from student where sname = ?";
			preparedStatement = connection.prepareStatement(sql);
			//查询学生表中学生姓名为hjh 的学生
			preparedStatement.setString(1, "hjh");
			//将sql语句查询的结果放入resultSet中
			resultSet = preparedStatement.executeQuery();
			while( resultSet.next()) {
				int sid = resultSet.getInt("sid");
				String sname = resultSet.getString("sname");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				//打印
				System.out.println("Student [sid=" + sid + ", sname=" + sname + ", age=" + age + ", gender=" + gender + "]");
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
		
			//不为空就关闭
			if(connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				connection = null;
			}
			
			//不为空就关闭
			if(preparedStatement != null) {
				try {
					preparedStatement.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				preparedStatement = null;
			}
			System.out.println("==========查询成功==========");
		}
		
	}
}

发布了15 篇原创文章 · 获赞 20 · 访问量 1915

猜你喜欢

转载自blog.csdn.net/OliverND/article/details/103469921