以下的文章主要介绍的是Java连接MySQL 数据库(以MySQL数据库为例),我们主要是以MySQL数据库为例讲下Java正确连接MySQL数据库的实际操作流程,以下就是对其详细内容的描述。 当然,首先要安装有JDK(一般是JDK1.5.X)。然后安装MySQL,这些都比较简单,具体过
以下的文章主要介绍的是Java连接MySQL 数据库(以MySQL数据库为例),我们主要是以MySQL数据库为例讲下Java正确连接MySQL数据库的实际操作流程,以下就是对其详细内容的描述。
当然,首先要安装有JDK(一般是JDK1.5.X)。然后安装MySQL,这些都比较简单,具体过程就不说了。配置好这两个环境后,下载JDBC驱动MySQL-connector-java-5.0.5.zip(这个是最新版的)。然后将其解压缩到任一目录。我是解压到D盘,然后将其目录下的MySQL-connector-java-5.0.5-bin.jar加到classpath里,具体如下:
“我的电脑”-> “属性” -> “高级” -> “环境变量”,在系统变量那里编辑classpath,将D:\MySQL-connector-java-5.0.5\MySQL-connector-java-5.0.5-bin.jar加到最后,在加这个字符串前要加“;”,以与前一个classpath区分开。然后确定。
环境配置好了,很简单。现在,先配置MySQL,设其用户名为“root”,密码为“root”。在命令行或用一个SQL的前端软件创建Database。
我是用SQLyog的前端软件来创建Database的。
先创接MySQL 数据库:
<ol class="dp-xml"><li class="alt"><span><span>CREATE DATABASE SCUTCS; </span></span></li></ol>
接着,创建表:
<ol class="dp-xml"> <li class="alt"><span><span>CREATE TABLE STUDENT </span></span></li> <li><span>( </span></li> <li class="alt"><span>SNO CHAR(7) NOT NULL, </span></li> <li><span>SNAME VARCHAR(8) NOT NULL, </span></li> <li class="alt"><span>SEX CHAR(2) NOT NULL, </span></li> <li><span>BDATE DATE NOT NULL, </span></li> <li class="alt"><span>HEIGHT DEC(5,2) DEFAULT 000.00, </span></li> <li><span>PRIMARY KEY(SNO) </span></li> <li class="alt"><span>); </span></li> </ol>
然后插入数据,可以用SQL语句insert into values (value1, value2, ...);
也可以用SQLyog来操作
好了,创建好了。
下面,我们来编写.java文件来演示一下如何访问MySQL数据库。
<ol class="dp-xml"> <li class="alt"><span><span>import java.sql.*; </span></span></li> <li><span>public class JDBCTest { </span></li> <li class="alt"><span>public static void main(String[] args){ </span></li> </ol>
驱动程序名
<ol class="dp-xml"><li class="alt"> <span><span>String </span><span class="attribute">driver</span><span> = </span><span class="attribute-value">"com.</span></span>MySQL<span><span class="attribute-value">.jdbc.Driver"</span><span>; </span></span> </li></ol>
URL指向要访问的数据库名scutcs
<ol class="dp-xml"><li class="alt"> <span><span>String </span><span class="attribute">url</span><span> = </span><span class="attribute-value">"jdbc:</span></span>MySQL<span><span class="attribute-value">://127.0.0.1:3306/scutcs"</span><span>; </span></span> </li></ol>
MySQL配置时的用户名
<ol class="dp-xml"><li class="alt"><span><span>String </span><span class="attribute">user</span><span> = </span><span class="attribute-value">"root"</span><span>; </span></span></li></ol>
MySQL配置时的密码
<ol class="dp-xml"> <li class="alt"><span><span>String </span><span class="attribute">password</span><span> = </span><span class="attribute-value">"root"</span><span>; </span></span></li> <li><span>try { </span></li> </ol>
加载驱动程序
<ol class="dp-xml"><li class="alt"><span><span>Class.forName(driver); </span></span></li></ol>
连续MySQL 数据库
<ol class="dp-xml"> <li class="alt"><span><span>Connection </span><span class="attribute">conn</span><span> = </span><span class="attribute-value">DriverManager</span><span>.getConnection(url, user, password); </span></span></li> <li><span>if(!conn.isClosed()) </span></li> <li class="alt"><span>System.out.println("Succeeded connecting to the Database!"); </span></li> </ol>
statement用来执行SQL语句
<ol class="dp-xml"><li class="alt"><span><span>Statement </span><span class="attribute">statement</span><span> = </span><span class="attribute-value">conn</span><span>.createStatement(); </span></span></li></ol>
要执行的SQL语句
<ol class="dp-xml"><li class="alt"><span><span>String </span><span class="attribute">sql</span><span> = </span><span class="attribute-value">"select * from student"</span><span>; </span></span></li></ol>
结果集
<ol class="dp-xml"> <li class="alt"><span><span>ResultSet </span><span class="attribute">rs</span><span> = </span><span class="attribute-value">statement</span><span>.executeQuery(sql); </span></span></li> <li><span>System.out.println("-----------------"); </span></li> <li class="alt"><span>System.out.println("执行结果如下所示:"); </span></li> <li><span>System.out.println("-----------------"); </span></li> <li class="alt"><span>System.out.println(" 学号" + "\t" + " 姓名"); </span></li> <li><span>System.out.println("-----------------"); </span></li> <li class="alt"> <span>String </span><span class="attribute">name</span><span> = </span><span class="attribute-value">null</span><span>; </span> </li> <li><span>while(rs.next()) { </span></li> </ol>
选择sname这列数据
<ol class="dp-xml"><li class="alt"><span><span class="attribute">name</span><span> = </span><span class="attribute-value">rs</span><span>.getString("sname"); </span></span></li></ol>
首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。然后使用GB2312字符集解码指定的字节数组
<ol class="dp-xml"><li class="alt"><span><span class="attribute">name</span><span> = </span><span class="attribute-value">new</span><span> String(name.getBytes("ISO-8859-1"),"GB2312"); </span></span></li></ol>
输出结果
<ol class="dp-xml"> <li class="alt"><span><span>System.out.println(rs.getString("sno") + "\t" + name); </span></span></li> <li><span>} </span></li> <li class="alt"><span>rs.close(); </span></li> <li><span>conn.close(); </span></li> <li class="alt"><span>} catch(ClassNotFoundException e) { </span></li> <li><span>System.out.println("Sorry,can`t find the Driver!"); </span></li> <li class="alt"><span>e.printStackTrace(); </span></li> <li><span>} catch(SQLException e) { </span></li> <li class="alt"><span>e.printStackTrace(); </span></li> <li><span>} catch(Exception e) { </span></li> <li class="alt"><span>e.printStackTrace(); </span></li> <li><span>} </span></li> <li class="alt"><span>} </span></li> <li><span>} </span></li> </ol>
接下来我们运行一下看下效果:
D:\testjdbc>javac JDBCTest.java
D:\testjdbc>java JDBCTest
Succeeded connecting to the Database!
执行结果如下所示:
学号 姓名
0104421 周远行
0208123 王义平
0209120 王大力
0309119 李 维
0309203 欧阳美林
哈哈,成功啦,以上的相关内容就是对java连接MySQL 数据库的介绍,望你能有所收获。