zoukankan      html  css  js  c++  java
  • MyEclipse通过JDBC连接MySQL数据库基本介绍

    转载自:http://www.jb51.net/article/31876.htm

    1.前提是MyEclipse已经能正常开发Java工程 
    2.安装MySQL 
    个人使用的是版本是 mysql-5.0.22-win32.zip 
    网址:http://www.mysql.com/downloads/mysql/#downloads 
    3.下载JDBC驱动 
    个人使用的是 mysql-connector-java-5.1.22.zip,所需要的就是解压缩之后其中的 mysql-connector-java-5.1.22-bin.jar 
    网址:http://www.mysql.com/downloads/connector/j/ 

    4.代码测试 

     1 package ts.jsj.lyh;
     2 
     3 import java.sql.*;
     4 
     5 /** */
     6 /**
     7  * 使用JDBC连接数据库MySQL的过程 DataBase:JSJ, table:student;
     8  * 
     9  * @author DuChangfeng 2008 09 18
    10  */
    11 public class JDBCTest {
    12 
    13     public static Connection getConnection() throws SQLException,
    14             java.lang.ClassNotFoundException {
    15         // 第一步:加载MySQL的JDBC的驱动
    16         Class.forName("com.mysql.jdbc.Driver");
    17 
    18         // 取得连接的url,能访问MySQL数据库的用户名,密码;jsj:数据库名
    19         String url = "jdbc:mysql://localhost:3306/mydata";
    20         String username = "root";
    21         String password = "";
    22 
    23         // 第二步:创建与MySQL数据库的连接类的实例
    24         Connection con = DriverManager.getConnection(url, username, password);
    25         return con;
    26     }
    27 
    28     public static void main(String args[]) {
    29         try {
    30             // 第三步:获取连接类实例con,用con创建Statement对象类实例 sql_statement
    31             Connection con = getConnection();
    32             Statement sql_statement = con.createStatement();
    33 
    34             /** */
    35             /************ 对数据库进行相关操作 ************/
    36             // 如果同名数据库存在,删除
    37             // sql_statement.executeUpdate("drop table if exists student");
    38             // 执行了一个sql语句生成了一个名为student的表
    39             // sql_statement.executeUpdate("create table student (id int not null auto_increment, name varchar(20) not null default 'name', math int not null default 60, primary key (id) ); ");
    40             // 向表中插入数据
    41             // sql_statement.executeUpdate("insert student values(1, 'liying', 98)");
    42             // sql_statement.executeUpdate("insert student values(2, 'jiangshan', 88)");
    43             // sql_statement.executeUpdate("insert student values(3, 'wangjiawu', 78)");
    44             // sql_statement.executeUpdate("insert student values(4, 'duchangfeng', 100)");
    45             // ---以上操作不实用,但是列出来作为参考---
    46 
    47             // 第四步:执行查询,用ResultSet类的对象,返回查询的结果
    48             String query = "select * from student";
    49             ResultSet result = sql_statement.executeQuery(query);
    50             /** */
    51             /************ 对数据库进行相关操作 ************/
    52 
    53             System.out.println("Student表中的数据如下:");
    54             System.out.println("------------------------");
    55             System.out.println("学号" + " " + "姓名" + " " + "数据成绩 ");
    56             System.out.println("------------------------");
    57 
    58             // 对获得的查询结果进行处理,对Result类的对象进行操作
    59             while (result.next()) {
    60                 int number = result.getInt("sno");
    61                 String name = result.getString("sname");
    62                 String mathScore = result.getString("sgrade");
    63                 // 取得数据库中的数据
    64                 System.out.println(" " + number + " " + name + " " + mathScore);
    65             }
    66 
    67             // 关闭连接和声明
    68             sql_statement.close();
    69             con.close();
    70 
    71         } catch (java.lang.ClassNotFoundException e) {
    72             // 加载JDBC错误,所要用的驱动没有找到
    73             System.err.print("ClassNotFoundException");
    74             // 其他错误
    75             System.err.println(e.getMessage());
    76         } catch (SQLException ex) {
    77             // 显示数据库连接错误或查询错误
    78             System.err.println("SQLException: " + ex.getMessage());
    79         }
    80     }
    81 
    82 }

    加上几点个人认为需要注意的地方: 

    1)关于mysql-connector-java-5.1.22-bin.jar 的存放位置。在MyEclipse具体的java工程中新建一存放jar 包的文件夹(如 lib),将mysql-connector-java-5.1.22-bin.jar 复制到文件夹中,选中jar包右击--->Build Path--->Add To Build Path,即可。 

    若出现 

    ClassNotFoundExceptioncom.mysql.jdbc.Driver 

    的提示,则正是由于缺少导入jar包所造成的。 

    2)如果已经对MySQL的使用很熟悉,则可忽略这条。个人在测试连接时,老是出现这样的异常提示: 

    SQLException: Communications link failure 
    The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. 

    这正是由于个人对MySQL使用不熟悉,对MySQL进行了诸多尝试性的操作,不知何时无意中将MySQL的服务(如果在安装MySQL时没有更改的话,缺省服务名就是MySQL)关闭,解决方法开启此服务即可。控制面板--->管理工具--->服务--->MySQL--->选择启用。 

    3)在使用上面的代码测试时,需要更改的地方有: 
    //MySQL数据库的用户名,密码,数据库名 

    1 String url = "jdbc:mysql://localhost:3306/jsj"; 
    2 String username = "root"; 
    3 String password = "111"; 

    以及具体基本表中的所要查询的字段名: 

    1 int number = result.getInt("sno"); 
    2 String name = result.getString("sname"); 
    3 String mathScore = result.getString("sgrade"); 
  • 相关阅读:
    C#static
    Sql中CHARINDEX用法
    分分钟用上C#中的委托和事件
    为什么使用抽象类?有什么好处?
    【NOIP】普及组2009 细胞分裂
    【VIJOS】P1512 SuperBrother打鼹鼠
    【NOIP】提高组2014
    @NOIP2018
    @NOIP2018
    @NOIP2018
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/5185754.html
Copyright © 2011-2022 走看看