当前位置:Gxlcms > 数据库问题 > JDBC

JDBC

时间:2021-07-01 10:21:17 帮助过:6人阅读

/** * @author Administrator * 演示示例1:使用纯Java方式连接数据 */ /** * @author Administrator alt+shift+j */ public class demo1 { public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println("建立连接失败"); } try { conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/hello?useUnicode=true&characterEncoding=utf-8", "root", "123456"); System.out.println("建立连接成功"); // 3.创建ps ,代表预编译的sql对象 ps = conn.prepareStatement("select* from student"); // 4.执行 rs = ps.executeQuery(); while (rs.next()) { System.out.println(rs.getInt("id") + " " + rs.getString("name") + rs.getFloat("Chinese")); } } catch (SQLException e) { e.printStackTrace(); System.out.println("建立连接失败"); } finally { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } if (rs != null) { try { rs.close(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } rs = null; } if (ps != null) { try { ps.close(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } ps = null; } } } } View Code

 技术图片

 

JDBC

标签:void   catch   com   pen   方式   str   final   stat   demo   

人气教程排行