package com.xatu.mysql;
import java.sql.*;
public class TestJDBC {
public static void main(String[] args) {
Connection connection = null;
try {
//加载使用数据库的驱动
Class.forName("com.mysql.jdbc.Driver");
//使用数据库的路径
String url = "jdbc:mysql://localhost:3306/test";
//连接数据库
String name = "root";
String passwd = "123456";
connection = DriverManager.getConnection(url,name,passwd);
System.out.println("连接成功...");
//获取数据库对象
Statement statement = connection.createStatement();
//执行数据库语句
String sql = "SELECT * FROM t999";
//得到返回结果
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
System.out.println(resultSet.getString(1));
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
//退出连接
try {
if (connection != null){
connection.close();
System.out.println("连接关闭...");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
MySql——JDBC使用
最新推荐文章于 2024-02-04 11:08:02 发布