sql语句查询数据库案例

时间:2023-03-09 16:08:52
sql语句查询数据库案例
package com.hanqi.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import com.hanqi.util.JdbcConnectionUtil; public class Test { private static Connection con; //链接数据库
private static PreparedStatement pste;// 执行sql语句
private static ResultSet rs; //执行返回结果 public static void main(String[] args) {
init();
String sql = "select * from appuser";
try {
pste = con.prepareStatement(sql);// 执行sql语句
rs = pste.executeQuery();//执行完的返回结果 while(rs.next()) {
System.out.println(rs.getInt("id"));
System.out.println(rs.getString("username"));
System.out.println(rs.getString("password"));
System.out.println(rs.getString("realname"));
System.out.println(rs.getDate("createtime"));
System.out.println("================================");
} } catch (SQLException e) {
e.printStackTrace();
}
close();
} public static void init() {
con = JdbcConnectionUtil.getConnection();
} public static void close() {
JdbcConnectionUtil.destroy(con,pste,rs);
}
}