package jdbc;import java.awt.GradientPaint;import java.sql.*;public class conn { static Connection con; static Statement sql; static ResultSet res; public Connection getConnection(){ try { Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_hotel","root","123456"); System.out.println(conn); } catch (ClassNotFoundException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return con; } public static void main(String[] args) { conn c = new conn(); con = c.getConnection(); try { sql = con.createStatement(); //空指针异常 res = sql.executeQuery("select*from tb_room"); while(res.next()){ String id = res.getString("id"); String room = res.getString("room"); String price = res.getString("price"); String information = res.getString("information"); System.out.println("id"); System.out.println("room"); System.out.println("price"); System.out.println("information"); } } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } }}
添加回答
举报
0/150
提交
取消