• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
huangui
博客园    首页    新随笔    联系   管理    订阅  订阅
JDBC

使用JDBC,可以在ide中通过java对数据库进行操作

//jdbc
//1.加载驱动
public class jdbcFirstDemo{
public static void main(String[] args)throws ClassNotFoundException,SQLException{
Class.forName("com.mysql.jdbc.Driver");
//2.用户信息和url
//useUnicode=true&characterEncoding=utf8&useSSL=true
String url="jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=utf8&useSSL=true
String usrname="root";
String password="password"
//3.连接成功,数据库对象  Connection 代表数据库
Connection connection=DriverManager.getConnection(url,username,password);
//4.执行SQL的对象Statement执行sql的对象
Statement statement=connection.createStatement();
//5.执行SQL的对象去执行SQL,可能存在结果,查看返回结果
String sql="SELECT * FROM table";

ResultSet resultSet=Statement.executeQuery();//返回的结果集,结果集中封装了我们全部的查询出来的对象
while(resultSet.next()){
    System.out.println("id="+resultSet.getObject(columnLabel:"id");//列名
    
}
//6.释放连接
resultSet.close();
statement.close();
Connection.close();
}
}

 

posted on 2020-05-01 14:47  huangui  阅读(127)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3