zoukankan      html  css  js  c++  java
  • JDBC使用Demo

     1 package com.dascom.xue;
     2 
     3 import java.sql.DriverManager;
     4 import com.mysql.jdbc.*;
     5 
     6 
     7 public class DBDemo {
     8 
     9     /**
    10      * @param args
    11      */
    12     public static void main(String[] args) {
    13         // TODO 自动生成的方法存根
    14         
    15         String url = "jdbc:MySQL://192.168.20.242:3306/test";
    16         String user = "root";
    17         String pwd = "test";
    18         
    19         try 
    20         {
    21             //加载驱动
    22             Class.forName("com.mysql.jdbc.Driver");
    23             //连接数据库
    24             Connection conn =  (Connection) DriverManager.getConnection(url, user, pwd);
    25             
    26             if (!conn.isClosed())
    27             {
    28                 System.out.println("Connect DB success");
    29             }
    30             //创建执行sql的对象
    31             Statement statment =  (Statement) conn.createStatement();
    32             
    33             String sql = "select * from wanbu_data_user where userid=7";
    34             //执行sql并返回结果集
    35             ResultSet rs = (ResultSet) statment.executeQuery(sql);
    36             
    37             while (rs.next())
    38             {
    39                 System.out.println(rs.getString("username"));
    40             }
    41             conn.close();
    42         } catch (ClassNotFoundException e) {
    43             e.printStackTrace();
    44         }catch (Exception e1)
    45         {}
    46     }
    47 
    48 }
  • 相关阅读:
    python 之字符编码
    python文件处理
    迭代器和生成器
    内置函数和匿名函数
    函数之递归
    函数 之装饰器
    python 函数进阶与闭包
    python 之 函数
    python之运算符
    python字符串内置方法
  • 原文地址:https://www.cnblogs.com/codingrabbit/p/3461894.html
Copyright © 2011-2022 走看看