zoukankan      html  css  js  c++  java
  • Java操作Mysql笔记

    第一步,需要下载JDBC驱动, 点我。然后选择合适的版本即可。

    下载完成之后解压,然后将mysql-connector-java-5.1.6-bin.jar文件放到java的安装目录下面。

    这里每个人的安装路径不同,需要找到自己的路径。

    第二步,需要在mysql下新建一个数据库,这个很简单,比如create database test; 

    然后use test;来切换当前正在使用的数据库。

    还需要新建一张表,因为下面我们的一些操作是在表上进行的。

    第三步,加载JDBC驱动,连接数据库。

    下面代码仅供参考,在我的机器上可以执行。

      1 import java.sql.*;
      2 import java.util.jar.JarException;
      3 
      4 public class DBManager {
      5     // 用户名
      6     private String user = "";
      7     // 密码
      8     private String password = "";
      9     // 主机
     10     private String host = "";
     11     // 数据库名字
     12     private String database = "";
     13     /*
     14      * 
     15      * private String
     16      * url="jdbc:mysql://"+host+"/"+"useUnicode=true&characterEncoding=GB2312";
     17      */
     18     private String url = "";
     19     private Connection con = null;
     20     Statement stmt;
     21     /**
     22      * 
     23      * 根据主机、数据库名称、数据库用户名、数据库用户密码取得连接。
     24      * 
     25      * @param host
     26      *            String
     27      * 
     28      * @param database
     29      *            String
     30      * 
     31      * @param user
     32      *            String
     33      * 
     34      * @param password
     35      *            String
     36      */
     37     public DBManager(String host, String database, String user, String password) {
     38         this.host = host;
     39         this.database = database;
     40         this.user = user;
     41         this.password = password;
     42         // 显示中文
     43         this.url = "jdbc:mysql://" + host + "/" + database;
     44         try {
     45             Class.forName("com.mysql.jdbc.Driver");
     46         } catch (ClassNotFoundException e) {
     47             System.err.println("class not found:" + e.getMessage());
     48         }
     49         try {
     50             con = DriverManager.getConnection(this.url, this.user,
     51                     this.password);
     52             // 连接类型为ResultSet.TYPE_SCROLL_INSENSITIVE,
     53             // ResultSet.CONCUR_READ_ONLY
     54             stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
     55                     ResultSet.CONCUR_READ_ONLY);
     56         } catch (SQLException a) {
     57             System.err.println("sql exception:" + a.getMessage());
     58         }
     59     }
     60     /**
     61      * 
     62      * 返回取得的连接
     63      */
     64     public Connection getCon() {
     65         return con;
     66     }
     67     /**
     68      * 
     69      * 执行一条简单的查询语句
     70      * 
     71      * 返回取得的结果集
     72      */
     73     public ResultSet executeQuery(String sql) {
     74         ResultSet rs = null;
     75         try {
     76             rs = stmt.executeQuery(sql);
     77         } catch (SQLException e) {
     78             e.printStackTrace();
     79         }
     80         return rs;
     81     }
     82     /**
     83      * 
     84      * 执行一条简单的更新语句
     85      * 
     86      * 执行成功则返回true
     87      */
     88     public boolean executeUpdate(String sql) {
     89         boolean v = false;
     90         try {
     91             v = stmt.executeUpdate(sql) > 0 ? true : false;
     92         } catch (SQLException e) {
     93             e.printStackTrace();
     94         } finally {
     95             return v;
     96         }
     97     }
     98     public static void main(String[] args) throws java.lang.NullPointerException{
     99         ResultSet rs;
    100         DBManager exe = new DBManager("127.0.0.1", "test", "root", "118118");
    101 
    102         rs = exe.executeQuery("SELECT * FROM student");
    103         
    104         try {
    105             ResultSetMetaData meta_data = rs.getMetaData();//列名
    106             for (int i_col = 1; i_col <= meta_data.getColumnCount(); i_col++) {
    107                 System.out.print(meta_data.getColumnLabel(i_col) + "   ");
    108             }
    109             System.out.println();
    110             while (rs.next()) {
    111                 for (int i_col = 1; i_col <= meta_data.getColumnCount(); i_col++) {
    112                     System.out.print(rs.getString(i_col) + "  ");
    113                 }
    114                 System.out.println();
    115             }
    116         } catch (Exception e) {
    117 
    118         }
    119     }
    120 }
    View Code
  • 相关阅读:
    移动H5页面微信支付踩坑之旅(微信支付、单页面路由模拟、按钮加锁、轮询等常见功能)
    EDM模板编写踩坑指南(持续更新中)
    7天内我面试了10家公司,如何从命中率0%到命中率至70%?
    codewars.DNA题目几种解法分析(字符串替换)
    webpack+react搭建环境
    手机端访问网页 js根据浏览器区别转入手机端网址的URL
    树状数组————(神奇的区间操作)蒟蒻都可以看懂,因为博主就是个蒟蒻
    DFS————从普及到IOI(暴力骗分小能手)
    Manacher(马拉车)————O(n)回文子串
    离散化————实现梦想的算法
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/4192266.html
Copyright © 2011-2022 走看看