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 }
  • 相关阅读:
    4815 江哥的dp题a
    CON1023 明明的计划
    5200 fqy的难题----2的疯狂幂
    [SCOI2005] 最大子矩阵
    1457 又是求和?
    2064 最小平方数
    vijos P1459车展
    1366 xth 的第 12 枚硬币
    1360 xth 的玫瑰花
    3223: Tyvj 1729 文艺平衡树
  • 原文地址:https://www.cnblogs.com/codingrabbit/p/3461894.html
Copyright © 2011-2022 走看看