jdbc方式mysql语句增删改查

import java.sql.*;

public class CRUD {
	public static void main(String[] args) throws SQLException {
		select();
		System.out.println("-------------------------");
		insert();
		select();
		System.out.println("-------------------------");
		delete();
		select();
		System.out.println("-------------------------");
		update();
		select();
		System.out.println("-------------------------");
	}

	public static void select() throws SQLException {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		DBUtil dbUtils = new DBUtil();
		try {
			connection = dbUtils.getConnection();
			String sql = "select * from studentinfo";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			// System.out.println("=================");
			while (resultSet.next()) {
				System.out.println(resultSet.getInt("student_id") + "   " + resultSet.getString("student_name"));
			}
			// dbUtils.closeDBResouse(connection, preparedStatement, resultSet);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	public static void insert() {
		DBUtil dbUtil = new DBUtil();
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			conn = dbUtil.getConnection();
			String sql = "insert into studentinfo (student_id,student_name,student_pwd) values (1,'wwwwww','1233')";
			stmt = conn.prepareStatement(sql);
			int count = stmt.executeUpdate(sql);
			System.out.println("插入了" + count + "条语句");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void delete() {
		DBUtil dbUtil = new DBUtil();
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			conn = dbUtil.getConnection();
			String sql = "delete from studentinfo where student_id = 1";
			stmt = conn.prepareStatement(sql);
			int count = stmt.executeUpdate(sql);
			System.out.println("您删除了" + count + "条语句");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void update() {
		DBUtil dbUtil = new DBUtil();
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			conn = dbUtil.getConnection();
			String sql = "update studentinfo set student_name='sakura' where student_id = 5";
			stmt = conn.prepareStatement(sql);
			int count = stmt.executeUpdate(sql);
			System.out.println("您修改了" + count + "条语句");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

  

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

public class DBUtil {
    static Connection conn = null;
	static Statement stmt = null;
	static ResultSet rs= null;
	static String url ="jdbc:mysql://localhost:3306/tyzx1?useUnicode=true&characterEncoding=UTF-8";
	
	public Connection getConnection(){
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url,"root","2013005488");
			System.out.println("success");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
		
	}
}

  

posted @ 2016-08-18 16:16  杨玲枝  阅读(551)  评论(0编辑  收藏  举报