package jdbc;
import java.sql.*;
public class JdbcDemo{
public static void JdbcStep(){
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try{
//加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//连接数据库库
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/scott?user=root&password=100521&useSSL=true");
//创建命令
statement = connection.createStatement();
//查询
resultSet = statement.executeQuery("select deptno,dname from dept");
//结束处理
while(resultSet.next()){
int deptno = resultSet.getInt("deptno");
String dname = resultSet.getString("dname");
String row = String.format(
"deptno=%d,dname=%s",deptno,dname
);
System.out.println(row);
}
}catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
JdbcStep();
}
}