zoukankan      html  css  js  c++  java
  • java学习笔记--JDBC实例

    使用的数据库:

    mysql> use xiao;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A

    Database changed
    mysql> show tables;
    +----------------+
    | Tables_in_xiao |
    +----------------+
    | students       |
    +----------------+
    1 row in set (0.00 sec)

    mysql> desc students;
    +-------+---------------------+------+-----+---------+----------------+
    | Field | Type                | Null | Key | Default | Extra          |
    +-------+---------------------+------+-----+---------+----------------+
    | id    | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
    | name  | char(8)             | NO   |     | NULL    |                |
    | sex   | char(4)             | NO   |     | NULL    |                |
    | age   | tinyint(3) unsigned | NO   |     | NULL    |                |
    | tel   | char(13)            | YES  |     | -       |                |
    +-------+---------------------+------+-----+---------+----------------+
    5 rows in set (0.04 sec)

    mysql> select * from students;
    +----+---------+-----+-----+-------------+
    | id | name    | sex | age | tel         |
    +----+---------+-----+-----+-------------+
    |  1 | xiaoye  | m   |  31 | 15712340618 |
    |  2 | heilong | m   |  31 | 13312342130 |
    |  3 | baba    | m   |  60 | 13312348223 |
    |  4 | xiaoye  | m   |  30 | 17712341103 |
    +----+---------+-----+-----+-------------+
    4 rows in set (0.00 sec)

    mysql>

    java例子:

    import com.mysql.jdbc.Driver;

    import java.sql.*;

    public class JDBCTest {
    public static void main(String[] args) {
    Connection connection = new JDBCTest().getConnection(); //调用getConnection方法
    try {
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("select * from students;"); //executeQuery executeUpdate execute 的区别
    while(resultSet.next()) {  //循环resultSet,调用next()移动至第一条数据,没有hasnext()方法;
    int id = resultSet.getInt(1);
    String name = resultSet.getString(2);
    String sex = resultSet.getString(3);
    int age = resultSet.getInt(4);
    String tel = resultSet.getString(5);
    System.out.println(id + " " + name + " " + sex + " " + age + " " + tel + ";");
    }

    } catch (SQLException e) {
    e.printStackTrace();
    }finally {
    try {
    connection.close();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    }

    public Connection getConnection() {
    try {
    Class.forName("com.mysql.jdbc.Driver");  //加载驱动
    return DriverManager.getConnection("jdbc:mysql://localhost:3306/xiao","root","×××××××"); //链接数据库 注意ruturn
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;  //return
    }
    }
  • 相关阅读:
    Navicat 回复 psc 文件 Mysql
    android SDK 更新问题完美解决 http://dl-ssl.google.com refused
    利用android来赚钱
    苹果应用 Windows 申请 普通证书 和Push 证书 Hbuilder 个推(2)
    苹果应用 Windows 申请 普通证书 和Push 证书 Hbuilder 个推
    Java version 32转64位
    浅谈Jquery中的bind()、live()、delegate()、on()绑定事件方式
    JavaScript之深浅拷贝
    关于一道JS面试题的思考
    EasyUI-Tooltip(提示框)学习
  • 原文地址:https://www.cnblogs.com/xiaonihao444/p/8971849.html
Copyright © 2011-2022 走看看