原生jdbc
package com.mm.test;
import org.junit.Test;
import java.sql.*;
import java.util.Objects;
public class Demo1 {
private static String url="jdbc:mysql://localhost:3306/mk";
private static String username="root";
private static String password="root";
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, username, password);
PreparedStatement preparedStatement = connection.prepareStatement("select * from t_cuser");
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()){
int anInt = rs.getInt(1);
System.out.println(anInt);
}
if (!Objects.isNull(rs)){
rs.close();
}
if (!Objects.isNull(preparedStatement)){
preparedStatement.close();
}
if (!Objects.isNull(connection)){
connection.close();
}
}
@Test
public void testInsert(){
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, username, password);
preparedStatement = connection.prepareStatement("insert into t_cuser(cname) values('奢侈品')");
int i = preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if (!Objects.isNull(preparedStatement)){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (!Objects.isNull(connection)){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testDelect(){
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, username, password);
preparedStatement = connection.prepareStatement("delete from t_cuser where cid=6");
int i = preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if (!Objects.isNull(preparedStatement)){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (!Objects.isNull(connection)){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testUpdate(){
Connection connection=null;
PreparedStatement preparedStatement=null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, username, password);
preparedStatement = connection.prepareStatement("update t_cuser set cname='奢侈品' where cid=5");
int i = preparedStatement.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if (!Objects.isNull(preparedStatement)){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (!Objects.isNull(connection)){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testSelect(){
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection(url,username,password);
preparedStatement=connection.prepareStatement("select * from t_cuser");
rs = preparedStatement.executeQuery();
while (rs.next()){
int anInt = rs.getInt(1);
String an = rs.getString(2);
System.out.println(anInt+"-"+an);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if (!Objects.isNull(rs)){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (!Objects.isNull(preparedStatement)){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (!Objects.isNull(connection)){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}