<p class="intro">今天尝试建Java通过JDBC连接到MySQL上,测试成功了,总结一下连接的具体方法。 Fristly:首先必须要安装有JDK(JDK在官网可下,我用的是JDK1.8.0_60),安装时注意环境变量的配置。 Second:下载安装MySQL,安装过程很简单就不细说了,注意在安装过程中会设置</p> <span style="color:rgb(192,189,187); font-family:'Microsoft YaHei','Helvetica Neue',SimSun; font-size:14px; line-height:21px; background-color:rgb(0,0,0)"><span style="font-family:Microsoft YaHei; word-wrap:normal; word-break:normal">今天尝试建Java通过JDBC连接到MySQL上,测试成功了,总结一下连接的具体方法。</span></span><span style="color:rgb(192,189,187); font-family:'Microsoft YaHei','Helvetica Neue',SimSun; font-size:14px; line-height:21px; background-color:rgb(0,0,0)"></span> <span style="font-family:Microsoft YaHei; word-wrap:normal; word-break:normal"><strong>Fristly:首先必须要安装有JDK(JDK在官网可下,我用的是JDK1.8.0_60),安装时注意环境变量的配置。</strong></span> <span style="font-family:Microsoft YaHei; word-wrap:normal; word-break:normal"><strong><br> </strong></span> <span style="font-family:Microsoft YaHei; word-wrap:normal; word-break:normal"><strong>Second:下载安装MySQL,安装过程很简单就不细说了,注意在安装过程中会设置用户名和密码,中呜嗷记住,在使用MySQL和用Java连接到MySQL都会用到,我的用户名和密码都是“root”。</strong></span> <span style="font-family:Microsoft YaHei; word-wrap:normal; word-break:normal"><strong><br> </strong></span> <span style="word-wrap:normal; word-break:normal"><strong><span style="font-family:SimSun; word-wrap:normal; word-break:normal">Third:下载JDBC驱动包</span></strong></span><span style="font-family:SimSun; word-wrap:normal; word-break:normal"><strong>mysql-connector-java-5.1.26.zip这个是比较新的版本,去官网便可下载,将其解压到任意地方(我是解压到D盘的JDK文件夹下了,建议解压到你安装的JDK文件夹下),载解压文件夹里找到mysql-connector-java-5.1.26-bin.jar <wbr>复制其路径。添加路径到classpath里.(classpath不区分大小写)。具体方法如下:</wbr></strong></span> <span style="font-family:SimSun; word-wrap:normal; word-break:normal"><strong>“我的电脑”——> “右键属性”——> “高级系统设置”——> “环境变量” 在系统变量里找到classpath 点击编辑,在最后面添加 D:\JDK\mysql-connector-java-5.1.26\mysql-connector-java-5.1.26-bin.jar 在加这个字符串前要加“;”,以与前一个classpath区分开。然后确定。</strong></span> <strong><br> </strong> <strong>Fourth:利用MySQL创建一个数据库,我已有一个名为students的数据库。</strong> <strong><br> </strong> <strong>Fifth:最后就可以编写.java程序了,下面是我写的一个连接MySQL的程序和部分分析。</strong> <strong>//需要加载的驱动程序名</strong> <strong>private static final String DataBaseDriver = "com.mysql.jdbc.Driver";</strong> <strong>//URL指向要访问的数据库名,我的数据库名为students</strong> <strong>private static final String DataBaseURL = "jdbc:mysql://localhost:3306/students";</strong> <strong>//安装MySQL时设置的用户名</strong> <strong>private static final String DataBaseUser = "root";</strong> <strong>//安装MySQL是设置的密码</strong> <strong>private static final String DataBasePassword = "root";</strong> <strong><br> </strong> <strong>//加载MySQL数据库驱动</strong> <img src="http://s1.sinaimg.cn/mw690/0067Jqkdty6Wvs8Xdss00&690" style="max-width:90%" style="max-width:90%" name="image_operate_69401445847895376" alt="Java通过JDBC连接到MySQL" title="Java通过JDBC连接到MySQL" style="margin:0px; padding:0px; border:0px; list-style:none"> <strong><br> </strong> <strong>//连接MySQL数据库</strong> <img src="http://s6.sinaimg.cn/mw690/0067Jqkdty6Wvsrkm4Rd5&690" style="max-width:90%" style="max-width:90%" name="image_operate_83031445847901948" alt="Java通过JDBC连接到MySQL" title="Java通过JDBC连接到MySQL" style="margin:0px; padding:0px; border:0px; list-style:none"><br> <br> <strong>source file:</strong> <br> <img src="http://s16.sinaimg.cn/mw690/0067Jqkdty6WvsH8Qp18f&690" style="max-width:90%" style="max-width:90%" name="image_operate_97421445847938008" alt="Java通过JDBC连接到MySQL" title="Java通过JDBC连接到MySQL" style="margin:0px; padding:0px; border:0px; list-style:none"> <br> <wbr><strong>运行结果:</strong> <img src="http://s8.sinaimg.cn/mw690/0067Jqkdty6WxaUKWKH47&690" style="max-width:90%" style="max-width:90%" name="image_operate_56271445941018404" alt="Java通过JDBC连接到MySQL" title="Java通过JDBC连接到MySQL" style="margin:0px; padding:0px; border:0px; list-style:none"><br> <br> <strong>相关下载链接:</strong> <strong>JDK下载:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html</strong> <strong>eclipse下载:http://www.eclipse.org/downloads/</strong> <strong>MySQL下载:http://dev.mysql.com/downloads/mysql/</strong> <strong>Connector/J 下载:http://dev.mysql.com/downloads/connector/j/</strong> </wbr>