1. 程式人生 > >JDBC之 Statement CRUD(3)

JDBC之 Statement CRUD(3)

免費錄播jdbc視訊

程式碼

package net.mmr.jdbc.t1;

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

import net.mmr.jdbc.t1.util.JdbcUtil;

import org.junit.Test;

public class TestCURD {

    //刪除
    @Test
    public void delete() throws Exception{

        Class.forName("com.mysql.jdbc.Driver"
); Connection conn = DriverManager.getConnection("jdbc:mysql:///mmr", "root", "1234"); Statement st = conn.createStatement(); String sql = "delete from users where id = 'u004'"; int r = st.executeUpdate(sql); System.out.println(r); st.close(); conn.close(); } //更新
@Test public void update() throws Exception{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql:///mmr", "root", "1234"); Statement st = conn.createStatement(); String sql = "update users set password = '456' where id = 'u004'"
; int r = st.executeUpdate(sql); System.out.println(r); st.close(); conn.close(); } //錄入 @Test public void create() throws Exception{ //1 註冊驅動 Class.forName("com.mysql.jdbc.Driver"); //2獲得連結 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mmr", "root", "1234"); //3獲得語句執行者 Statement st = conn.createStatement(); // 準備sql String sql = "insert into users(id,username,password) values('u004','feng','123')"; //4執行sql int r = st.executeUpdate(sql); //5處理結果 System.out.println(r); //6釋放資源 st.close(); conn.close(); } }