标签:jdbc 数据库连接 next 超过 要求 pac 索引 exec lis
为什么要进行数据库规范设计:
package com.model.test; import java.sql.*; public class JDBC { public static void main(String[] args) throws SQLException { Connection connection=null; Statement statement=null; ResultSet resultSet=null; try { //1.加载驱动 Class.forName("com.mysql.jdbc.Driver"); //2.获取用户的信息和url String url="jdbc:mysql://localhost:3306/shop"; String username="root"; String password="12346"; //3.获取数据库连接对象Connection connection = DriverManager.getConnection(url, username, password); //4.获取SQL执行对象Statement statement = connection.createStatement(); //5.获取SQL语句 String sql="select * from user"; //6.执行SQL并获取返回值 resultSet = statement.executeQuery(sql); //7.遍历返回值result即可 while (resultSet.next()){ String id = (String) resultSet.getObject("id"); String name = (String) resultSet.getObject(2); System.out.println("id号:"+id+"姓名:"+name); } } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); }finally { //8.关闭连接 resultSet.close(); statement.close(); connection.close(); } } }
标签:jdbc 数据库连接 next 超过 要求 pac 索引 exec lis
原文地址:https://www.cnblogs.com/zzhAylm/p/14744556.html