配置pom.xml 导入mysql-connector-java:5.1.49
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
</dependencies>
5.使用JDBC *** 作数据库
package com.qzxiaofeng.jdbc;
import java.sql.*;
/**
* @author 刘晓峰
*/
public class FirstJdbc1 {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/xiao?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";
PreparedStatement statement = null;
ResultSet resultSet = null;
Connection connection=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(url,"root","123456");
statement = connection.prepareStatement("select * from t_manager");
resultSet = statement.executeQuery();
while (resultSet.next()){
System.out.println("id:"+resultSet.getInt("id"));
System.out.println("username:"+resultSet.getString("username"));
System.out.println("password:"+resultSet.getString("password"));
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
if(null!=resultSet){
try {
resultSet.close();
} catch (SQLException exception) {
exception.printStackTrace();
}
}
if(null!=statement){
try {
statement.close();
} catch (SQLException exception) {
exception.printStackTrace();
}
}
if(null!=connection){
try {
connection.close();
} catch (SQLException exception) {
exception.printStackTrace();
}
}
}
}
}
6.程序执行结果,执行了一条查询语句
完结,加油!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)