import java.sql.*;
/**
* JDBC测试
*/
public class JdbcTest {
/**
* main方法
*
* @param args
*/
public static void main(String[] args) {
try {
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//创建对象
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//创建连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123");
//创建连接对象
stmt = conn.createStatement();
//获取查询结果集
rs = stmt.executeQuery("select * from t_user");
//循环打印信息
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t"
+ rs.getString(3) + "\t" + rs.getInt(4));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭资源
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
JDBC测试
最新推荐文章于 2024-01-28 14:31:47 发布