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
原文地址:https://www.cnblogs.com/Stomach-ache/p/4192266.html