zoukankan      html  css  js  c++  java
  • java连接mysql

     1 import java.sql.*;
     2 
     3 public class MysqlTest {
     4     public static void main(String[] args) {
     5         // 驱动程序名
     6         String driver = "org.gjt.mm.mysql.Driver";
     7         // URL指向要访问的数据库名world
     8         // String url = "jdbc:mysql://127.0.0.1:3306/xh";
     9         String url = "jdbc:mysql://localhost/xh";
    10         // MySQL配置时的用户名
    11         String user = "xiaohengdada";
    12         // MySQL配置时的密码
    13         String password = "123456";
    14         String name;
    15         try {
    16             // 加载驱动程序
    17             Class.forName(driver);
    18             // 连续数据库
    19             Connection conn = DriverManager.getConnection(url, user, password);
    20             if (!conn.isClosed())
    21                 System.out.println("Succeeded connecting to the Database!");
    22             // statement用来执行SQL语句
    23             Statement statement = conn.createStatement();
    24             // 要执行的SQL语句
    25             String sql = "select * from students";
    26 
    27             // 结果集
    28             ResultSet rs = statement.executeQuery(sql);
    29             System.out.println(rs);
    30             System.out.println("Succeeded connecting to the Database!");
    31             while (rs.next()) {
    32                 // 选择Name这列数据
    33                 name = rs.getString("name");
    34                 // 输出结果
    35                 System.out.println(name);
    36             }
    37             rs.close();//关闭当前的结果集
    38             statement.close();//关闭statement对象以及所对应的结果集
    39             conn.close();//关闭当前的连接以及释放由它所创建的JDBC资源
    40         } catch (ClassNotFoundException e) {
    41             System.out.println("Sorry,can`t find the Driver!");
    42             e.printStackTrace();
    43         } catch (SQLException e) {
    44             e.printStackTrace();
    45         } catch (Exception e) {
    46             e.printStackTrace();
    47         }
    48     }
    49 }

    需要在mysql建立相关数据库及表。

    连接成功,会输出如下数据。

    具体的配置可以参考此链接;http://database.51cto.com/art/201006/204217.htm

    下面是获取表中id字段:

     1 import java.sql.*;
     2 
     3 public class MysqlTest
     4 {
     5     public static void main(String[] args)
     6     {
     7         String driver="org.gjt.mm.mysql.Driver";
     8         String url="jdbc:mysql://localhost/xh";
     9         String user="xiaohengdada";
    10         String password="123456";
    11         //int id;
    12         int id1;
    13     
    14     
    15     try
    16     {
    17         Class.forName(driver);
    18         Connection conn=DriverManager.getConnection(url,user,password);
    19         if (!conn.isClosed())
    20             System.out.println("Succeeded connecting to be Database!");
    21         
    22         Statement statement=conn.createStatement();
    23         ResultSet rs=statement.executeQuery("select * from students");
    24         
    25         System.out.println(rs);
    26         while(rs.next())
    27         {
    28             id1=rs.getInt("id");
    29             System.out.println(id1);
    30         }
    31         rs.close();
    32         statement.close();
    33         conn.close();    
    34     }
    35     catch(ClassNotFoundException e)
    36     {
    37         System.out.println("Sorry,can't find the Driver!");
    38         e.printStackTrace();
    39     }
    40     catch(SQLException e)
    41     {
    42         e.printStackTrace();
    43     }
    44     catch(Exception e)
    45     {
    46         e.printStackTrace();
    47     }
    48     }
    49 }

  • 相关阅读:
    (轻松一刻)神秘而又强大的蓝翔学院!而且还是当中最可怕的挖掘机系!
    IIS 反向代理二级目录及泛目录
    正则表达式匹配外链和内链
    关于 preg_replace_callbank 的学习记录
    Linux 笔记
    Linux 笔记
    Linux 笔记
    使用 Docker 安装 showdoc
    Docker 入门
    Linux 笔记
  • 原文地址:https://www.cnblogs.com/xh0102/p/5689576.html
Copyright © 2011-2022 走看看