Java中使用MySQL从安装、配置到实际程序测试详解
来源:互联网 发布:mac qq截图快捷键 编辑:程序博客网 时间:2024/06/05 08:27
By zieckey(http://blog.csdn.net/zieckey)
All Rights Reserved!
下载MySQL的JDBC jar包:mysql-connector-java-5.0.3-bin.jar
为了后面的方便这个选项要选项。
安装完后,下面测试下:
1. 运行MySQL Server
M:/Documents and Settings/apple>mysqld-nt --help
mysqld-nt Ver 5.0.22-community-nt for Win32 on ia32 (MySQL Community Edition (G
PL))
Copyright (C) 2000 MySQL AB, by Monty and others
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license
我们再运行客户端进行连接:
M:/WINDOWS/system32;M:/WINDOWS;M:/WINDOWS/System32/Wbem;M:/Program Files/MySQL/MySQL Server 5.0/bin
再运行 mysqld-nt 应该就没有问题了。
格式: mysql -h主机地址 -u用户名 -p用户密码
例1:连接到本机上的MYSQL。
首先在打开DOS窗口,然后进入目录 mysqlbin,再键入命令mysql -uroot -p,回车后提示你输密码,如果刚安装好MYSQL,超级用户root是没有密码的,故直接回车即可进入到MYSQL中了,MYSQL的提示符是:mysql>
Enter password:
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 1 to server version: 5.0.22-community-nt
mysql -h110.110.110.110 -uroot -pabcd123
(注:u与root可以不用加空格,其它也一样)
mysql> exit
Bye
1. 准数据库文件
创建一个LearnJava数据库:
M:/Documents and Settings/apple>mysql -uroot -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 6 to server version: 5.0.22-community-nt
Query OK, 1 row affected (0.02 sec)
mysql> use LearnJava;
Database changed
mysql> create table UserInfo(
-> UserName varchar (20) not null,
-> UserPwd varchar (20) not null
-> );
Query OK, 0 rows affected (0.06 sec)
插入一条数据:
mysql> insert into UserInfo values ('zieckey','123456');
Query OK, 1 row affected (0.03 sec)
这里我是将mysql-connector-java-5.0.3-bin.jar放在java的安装目录下,
然后导入到CLASSPATH环境变量中,例如:
.;%JAVA_HOME%/lib/tools.jar;%JAVA_HOME%/lib/mysql-connector-java-5.0.3-bin.jar;%JAVA_HOME%/lib/dt.jar;
/**
* 该程序用来测试JDBC和MySQL连接
*/
package test;
import java.sql.*;
public class QueryJDBC
{
static
{
try
{
//驱动和odbc不同
Class.forName ( "org.gjt.mm.mysql.Driver" );
System.out.println ( "success loading mysql Driver...." );
} catch ( Exception e )
{
System.out.println ( "Error loading mysql Driver....." );
e.printStackTrace ( );
}
}
/**
* @param args
*/
public static void main ( String agrs[] )
{
try
{
//连接参数与Access不同
String url = "jdbc:mysql://localhost/LearnJSP";
//建立连接
Connection con = DriverManager.getConnection ( url, "root", "011124" );
//建立发送SQL命令的Statement对象
Statement stmt = con.createStatement ( );
//返回查询结果
ResultSet rs = stmt.executeQuery ( "select * from userinfo" );
ResultSetMetaData md = rs.getMetaData ( );
int col = md.getColumnCount ( );
for ( int i = 1; i <= col; i++ )
{
System.out.println ( md.getColumnName ( i ) + "/t" );
}
while ( rs.next ( ) )
{
String strData = "";
for ( int i = 1; i <= col; i++ )
{
strData = strData + rs.getString ( i ) + "/t";
}
System.out.println ( strData );
}
//断开Connection连接
rs.close();
stmt.close();
con.close ( );
} catch ( SQLException e )
{
e.printStackTrace ( );
}
}
}
E:/JavaLesson/MySQLTest/test>javac QueryJDBC.java -d .
success loading mysql Driver....
UserName
UserPwd
zieckey 123456
该程序很可能运行出现异常,提示找不到org.gjt.mm.mysql.Driver,
这个可以通过 Project->Properties->Java Build Path->Libraries 选择 Add Extenal JARs,
然后就可以选择第三方jar包:mysql-connector-java-5.0.3-bin.jar
这样程序就可以正常运行了。
- Java中使用MySQL从安装、配置到实际程序测试详解
- Java中使用MySQL从安装、配置到实际程序测试详解
- 详解MySQL中SlowLog的实际配置
- 详解MySQL中SlowLog的实际配置
- Android Studio 从安装到配置使用
- Android Studio 从安装到配置使用
- Git 从安装配置到使用心得
- Sqoop安装配置与从mysql中导入数据到hbase
- Sqoop安装配置与从mysql中导入数据到hbase
- Java从入门到精通——数据库篇Mongo DB 安装启动及配置详解
- java连接MySQL(从驱动下载到测试)
- MySQL从安装到使用(运用JDBC技术访问)JAVA(Eclipse)
- 请描述MySql从安装到配置的全部详细过程(确保一台新电脑可以顺利使用mysql)
- mysql数据库的使用:从安装到使用。
- mysql 记录一次实际业务中使用到存储过程、游标
- java桌面程序从打包到安装的全过程
- 将数据(数据库对象 和 表中数据)从 mysql 迁移到sql server 的实际样例
- java存储过程实际使用-MYSQL-LINUX
- Windows Mobile开发工具介绍
- andyLib----javascript常用工具库
- 关于TIMER
- SQL text ntext image 小问题
- VisualStudio2005中没有Asp.net Web项目的问题(VS2005 SP1补丁包)
- Java中使用MySQL从安装、配置到实际程序测试详解
- 对*.mht文件结构的一个浅显分析
- 一个托盘程序演示 -闹钟 Alert
- C++字符串完全指引之一 —— Win32 字符编码
- 唐骏:我为什么不愿当Google中国总裁?
- CSOCKET文件传输函数
- 什么是TTY
- 报表服务中的编程
- 用VC设计托盘图标程序