MySQL: ubuntu10.10 下与 JDBC 操作
来源:互联网 发布:js作用域和this关键字 编辑:程序博客网 时间:2024/06/15 04:06
如果您写好JDBC连接MySQL的Java文件,之后敲java命令运行代码,报异常:
- Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
原因是找不到驱动,ok?! 这篇文章适合您。
1. 下载MySQL的驱动
mysql-connector-java-5.1.16.tar.gz
到官方网站下载吧!
2. 解压
- tar -zxvf mysql-connector-java-5.1.16.tar.gz
解压之后文件夹中存在文件mysql-connector-java-5.1.16-bin.jar
名字太长,可以修改文件名:
- mv mysql-connector-java-5.1.16-bin.jar mysql-connector.jar
3. jdk与该驱动
如果你不使用eclipse的话,使用一般Java编辑器可以copy该mysql-connector.jar文件到:
- /home/mark/android/Java/jdk1.6.0_24/jre/lib/ext
就是你的jdk安装目录下面的/Java/jdk1.6.0_24/jre/lib/ext
4. Eclipse与该驱动
如果使用eclips的话,在新建工程中右键选择Build Path / Add External Archives选择mysql-connector.jar文件即可。
5. 测试源代码
- package net.mark;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class ShowInfo {
- private static final String SQLEXCEPTION =
- "jdbc:mysql://127.0.0.1:3306/mydb?mytable=root&password=";
- private static final String SQL = "jdbc:mysql://127.0.0.1/mydb?";
- private static final String USER = "root";
- private static final String PASSWORD = "";
- public static void main(String[] args) throws Exception {
- System.out.println("userId info:");
- getInfoFromDatabase(SQL, USER, PASSWORD, 1);
- System.out.println("==================================");
- System.out.println("userName info:");
- getInfoFromDatabase(SQL, USER, PASSWORD, 2);
- // 报异常信息:Access denied for user ''@'localhost' to database 'mydb'
- getInfoFromDatabase(SQLEXCEPTION, 1);
- }
- /**
- * 获取数据库中表的数据信息
- *
- * @param sql
- * sql语句
- * @param user
- * 用户名
- * @param password
- * 用户密码
- * @param columnIndex
- * 行、列索引.1 代表获得userId,2 代表获得userName
- */
- public static void getInfoFromDatabase(String sql, String user, String password, int columnIndex) {
- Connection conn = null;
- Statement statement = null;
- ResultSet result = null;
- try {
- //Class.forName("com.mysql.jdbc.Driver").newInstance();
- Class.forName("com.mysql.jdbc.Driver");//加载类Driver
- conn = DriverManager.getConnection(sql, user, password);
- statement = conn.createStatement();
- result = statement.executeQuery("select * from mytable");
- while (result.next()) {
- System.out.println("userName= " + result.getString(columnIndex));
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (result != null) {
- result.close();
- result = null;
- }
- if (statement != null) {
- statement.close();
- statement = null;
- }
- if (conn != null) {
- conn.close();
- conn = null;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 获取数据库中表的数据信息,重载方法
- *
- * @param sql
- * sql语句
- * @param columnIndex
- * 行、列索引.1 代表获得userId,2 代表获得userName
- */
- public static void getInfoFromDatabase(String sql, int columnIndex) {
- Connection conn = null;
- Statement statement = null;
- ResultSet result = null;
- try {
- //Class.forName("com.mysql.jdbc.Driver").newInstance();
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(sql);
- statement = conn.createStatement();
- result = statement.executeQuery("select * from mytable");
- while (result.next()) {
- System.out.println("userName= " + result.getString(columnIndex));
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (result != null) {
- result.close();
- result = null;
- }
- if (statement != null) {
- statement.close();
- statement = null;
- }
- if (conn != null) {
- conn.close();
- conn = null;
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
6. 使用Eclipse看数据库
<1> 显示视图
window -- show view -- other
选择 Data Source Explorer和SQL Results
<2>在Data Source Explorer下面的Database Connections右键选择New...
<3> 选择MySQL数据库,Name任意取
<4> Next,填写数据库名称、用户名以及密码
这里数据库名称为mydb,用户root,密码为空。如果你的密码不为空的话,必须写!
<5> 上面图中,选择下载的MySQL的JDBC驱动。连接成功之后,显示如下:
<6> 显示表中数据。在表mytable右键Data选择Sample Contents
在SQL Results视图可以看到上述内容。
- Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database 'mydb'
- MySQL: ubuntu10.10 下与 JDBC 操作
- MySQL: ubuntu10.10 下与 JDBC 操作
- MySQL: ubuntu10.10 下数据备份与恢复
- MySQL: ubuntu10.10 下安装
- MySQL: ubuntu10.10 下安装
- MySQL: ubuntu10.10 下安装
- Ubuntu10.10下Apache+Php+Mysql安装
- ubuntu10.10下qt连接mysql数据库
- Ubuntu10.10下mysql+apache+PHP
- MySQL: ubuntu10.10 下配置环境变量
- MySQL: ubuntu10.10 下基本用法
- MySQL: ubuntu10.10下配置环境变量
- MySQL: ubuntu10.10下基本用法
- jdbc之mysql连接与操作
- ubuntu10.10环境下配置Apache+PHP+PHPmyadmin+MySQL
- Ubuntu10.10下安装黄金组合(Apache2+Mysql+php5+phpmyadmin)
- 详解Ubuntu10.10下Qt连接Mysql数据库
- ubuntu10.10下smbd服务配置——实现主机WinXP与虚拟机Ubuntu10.10共享
- mysql跨库操作速度慢的解决方案
- Ubuntu jsp平台使用JDBC来连接MySQL数据库
- cocos2d-x游戏开发 跑酷(三) 人物跑动
- 链表
- Ubuntu-13.04-X86_64安装GT630驱动
- MySQL: ubuntu10.10 下与 JDBC 操作
- HDU 1327 map
- 黑马程序员---银行业务调度系统
- C++编程 - 真的不要重复造轮子吗
- virtualenv
- bresenham画圆算法
- web.xml配置详解
- StringBuilder与StringBuffer的区别(转)
- 第十八篇 ANDROID的声音管理系统及服务