一、在MySQL中准备测试数据
1.执行命令
1.查询数据库
SHOW DATABASES;
2.创建数据库
CREATE DATABASE flinktest;
3.使用数据库
USE flinktest;
4.创建表格
CREATE TABLE Student
(
stuid INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
stuname VARCHAR(10) NOT NULL,
stuaddr VARCHAR(40) NOT NULL,
stusex VARCHAR(10) NOT NULL
);
5.插入数据
INSERT INTO Student(stuid,stuname,stuaddr,stusex)VALUES(1,"xiaoming","henan zhengzhou", "male")
INSERT INTO Student(stuid,stuname,stuaddr,stusex)VALUES(2,"xiaoqiang","shandong jinan", "female")
INSERT INTO Student(stuid,stuname,stuaddr,stusex)VALUES(3,"xiaohua","hebei shijiazhuang", "male")
INSERT INTO Student(stuid,stuname,stuaddr,stusex)VALUES(4,"xiaohong","yunnan kunming", "female")
6.查询数据
SELECT * FROM Student ;
2.执行效果
![这里写图片描述](https://img-blog.csdn.net/20171121091536381?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlndW9odWFCaWdkYXRh/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
二、测试JDBC连接
1.在pom.xml中添加驱动依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
2.定义Student实体类,用于封装数据
package code.book.stream.customsinkandsource.jdbc.java;
/**
* 用于存储数据库中的数据,作为bean使用
*/
public class Student {
int stuid;
String stuname;
String stuaddr;
String stusex;
public Student(int stuid, String stuname, String stuaddr, String stusex) {
this.stuid = stuid;
this.stuname = stuname;
this.stuaddr = stuaddr;
this.stusex = stusex;
}
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStuaddr() {
return stuaddr;
}
public void setStuaddr(String stuaddr) {
this.stuaddr = stuaddr;
}
public String getStusex() {
return stusex;
}
public void setStusex(String stusex) {
this.stusex = stusex;
}
@Override
public String toString() {
return "Student{" +
"stuid=" + stuid +
", stuname='" + stuname + '\'' +
", stuaddr='" + stuaddr + '\'' +
", stusex='" + stusex + '\'' +
'}';
}
}
2.执行JDBC测试程序
package code.book.stream.customsinkandsource.jdbc.java;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* 本类主要用于检测jdbc连接是否成功
*/
public class JdbcTest {
public static void main(String[] args) throws Exception {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://qingcheng11:3306/flinktest";
String username = "root";
String password = "qingcheng";
Connection connection = null;
Statement statement = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(
"select stuid,stuname,stuaddr,stusex from Student");
while (resultSet.next()) {
Student student = new Student(
resultSet.getInt("stuid"),
resultSet.getString("stuname").trim(),
resultSet.getString("stuaddr").trim(),
resultSet.getString("stusex").trim());
System.out.println(student);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
}
}
}
2.JDBC测试程序成功输出
Student{stuid=1, stuname='xiaoming', stuaddr='henan zhengzhou', stusex='male'}
Student{stuid=2, stuname='xiaoqiang', stuaddr='shandong jinan', stusex='female'}
Student{stuid=3, stuname='xiaohua', stuaddr='hebei shijiazhuang', stusex='male'}
Student{stuid=4, stuname='xiaohong', stuaddr='yunnan kunming', stusex='female'}