1. 程式人生 > 其它 >jdbc實現查詢和新增

jdbc實現查詢和新增

技術標籤:筆記jdbcmysql

jdbc實現查詢和新增

##1.1連線資料庫
程式碼如下(示例):

package dao;

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

public class Utils {
private static String DRIVER = "com.mysql.jdbc.Driver";
private static
String URL = "jdbc:mysql://localhost:3306/student?characterEncoding=utf8"; private static String USERNAME = "root"; private static String PWD = "root"; public static Connection getConnection() throws ClassNotFoundException, SQLException { Connection connection = null; Class.
forName(DRIVER); connection = DriverManager.getConnection(URL,USERNAME,PWD); return connection; } public static void closeAll(Connection connection,Statement statement,ResultSet resultSet) throws SQLException { if (resultSet != null) { resultSet.close(); } if (statement !=null) { statement.
close(); } if(connection !=null) { connection.close(); } } }

##1.2測試資料庫是否連線成功

程式碼如下(示例):

package dao;

import java.sql.Connection;
import java.sql.SQLException;

import model.Student;

public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
	Connection connection = Utils.getConnection();
	System.out.println(connection);
	
}
}

##1.3測試結果
在這裡插入圖片描述
#若輸出地址這表示有資料庫物件,連線成功。

##2.1獲得連結物件,對資料庫進行DML操作

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import model.Student;

public class StudentDao extends Utils{
//返回所有學生的集合
	public static ArrayList<Student> getAllStudent() throws SQLException, ClassNotFoundException {
		ArrayList<Student> students = new ArrayList<>();
		Connection connection = Utils.getConnection();
		Statement statement = connection.createStatement();
		ResultSet resultSet = statement.executeQuery("select * from stu");
		while(resultSet.next()) {
			Student student = new Student();
			student.setStuNo(resultSet.getString(1));
			student.setName(resultSet.getString(2));
			student.setSex(resultSet.getString(3));
			student.setBirthday(resultSet.getString(4));
			student.setIntroduce(resultSet.getString(5));
			student.setSalary(resultSet.getInt(6));
			students.add(student);
		}
		closeAll(connection, statement, resultSet);
		return students;
	}
	//向列表中新增某學生
	public static int insertStu(Student stu) throws ClassNotFoundException, SQLException {
		Connection connection = Utils.getConnection();
		String sql = "insert into stu values(?,?,?,?,?,?)";
		PreparedStatement pStatement = connection.prepareStatement(sql);
		pStatement.setString(1,stu.getStuNo() );
		pStatement.setString(2,stu.getName() );
		pStatement.setString(3,stu.getSex() );
		pStatement.setString(4,stu.getBirthday());
		pStatement.setString(5,stu.getIntroduce() );
		pStatement.setInt(6,stu.getSalary() );
		
		//closeAll(connection, pStatement, null);
		return pStatement.executeUpdate();
	}
		//更新資料
	public static int update(Student stu) throws ClassNotFoundException, SQLException {
		Connection conn = Utils.getConnection();
		String sql = "UPDATE stu set name=?,sex=?,birthday=?,introduce=?,salary=? WHERE stuNo= ?;";
		PreparedStatement pStatement =conn.prepareStatement(sql);
		pStatement.setString(1, stu.getName());
		pStatement.setString(2, stu.getSex());
		pStatement.setString(3, stu.getBirthday());
		pStatement.setString(4, stu.getIntroduce());
		pStatement.setDouble(5, stu.getSalary());
		pStatement.setString(6, stu.getStuNo());
		return pStatement.executeUpdate();
		
	} 
	//通過stuNo刪除一行
	public static int delete(int id) throws ClassNotFoundException, SQLException {
		Connection conn = Utils.getConnection();
		String sql = "delete from stu where stuNo = ?";
		PreparedStatement pStatement =conn.prepareStatement(sql);
		pStatement.setInt(1, id);
		return pStatement.executeUpdate();
		
	}
	
	
}

##2.2測試程式碼

package dao;

import java.sql.Connection;
import java.sql.SQLException;

import model.Student;

public class Test {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
	//Connection connection = Utils.getConnection();
	//System.out.println(connection);
	System.out.println(StudentDao.getAllStudent());
	Student stu = new Student("04","周芷若","女","2020-12-10","仙氣飄飄",5000);
	System.out.println(StudentDao.insertStu(stu));
	//	Student student = new Student();
//	student.setName("張三");
//	student.setSex("男");
//	student.setBirthday("2020-12-10");
//	student.setIntroduce("眾所周知");
//	student.setSalary(1200);
//	student.setStuNo("04");
//	
//	System.out.println(StudentDao.update(student));
	System.out.println(StudentDao.delete(03));
	
}
}


##2.3測試結果
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述

查詢,新增,更新,刪除,成功


總結

** 連線jdbc,操作增刪改查,測試是否成功,在以後的學習中可以直接拿來用,較為方便。**