package com.cskaoyan.JDBCDemo;
import org.junit.Test;
import java.sql.*;
public class JDBCStatement {
public Connection createConnetion() throws SQLException {
Connection connection = null;
String url = "jdbc:mysql://localhost:3306/db3";
String user = "root";
String pwd = "yang19960421";
connection = DriverManager.getConnection(url, user, pwd);
return connection;
}
@Test
public void createDatabases() throws SQLException {
Connection connection = createConnetion();
Statement statement = connection.createStatement();
String sql = "create database if not exists db3";
statement.execute(sql);
}
@Test
public void createTable() throws SQLException {
Connection connection = createConnetion();
Statement statement = connection.createStatement();
String sql = "create table if not exists tb1 (id int primary key,a int)";
statement.execute(sql);
}
@Test
public void insertInto() throws SQLException {
Connection connection = createConnetion();
Statement statement = connection.createStatement();
String sql = "insert into tb1 values(1,2),(2,33),(3,44)";
statement.execute(sql);
}
@Test
public void selectData() throws SQLException {
Connection connetion = createConnetion();
Statement statement = connetion.createStatement();
String sql = "select * from tb1";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
int id = resultSet.getInt("id");
int a = resultSet.getInt("a");
System.out.println(id+" "+ a);
}
}
}