Eclipse连接MySQL数据库
来源:互联网 发布:超越电子教室软件 编辑:程序博客网 时间:2024/06/06 00:53
转载出处:http://www.cnblogs.com/fnng/archive/2011/07/18/2110023.html
本来不想写这么简单人文章,在百度上搜索我这个标题,完全符合标题的一大堆。但我按照那些文章捣鼓了很久,就是不行。
我的环境:MySQL:mysql-essential-5.1.51-win32
jdbc驱动:我已经上传到csdn上一个:http://download.csdn.net/source/3451945
Eclipse:任意版本,免费的,可以百度的到。
1。MySQL安装,不会的朋友可以看连接:http://www.duote.com/tech/1/2430_1.html
下面来创建一个数据:
mysql>CREATE DATABASE test; //创建一个数据库mysql>use test; //指定test为当前要操作的数据库mysql>CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); //创建一个表user,设置两个字段。mysql>INSERT INTO user VALUES('huzhiheng','123456'); //插入一条数据到表中
2。打开Eclipse,创建一个项目(my),
操作:右键点击my--->build Path--->add external Archiver...选择jdbc驱动,点击确定。
我的项目列表:
3。驱动已经导入,下面我们来写一个程序验证一下
import java.sql.*;publicclass MysqlJdbc { publicstaticvoid main(String args[]) { try { Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序 //Class.forName("org.gjt.mm.mysql.Driver"); System.out.println("Success loading Mysql Driver!"); } catch (Exception e) { System.out.print("Error loading Mysql Driver!"); e.printStackTrace(); } try { Connection connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test","root","198876"); //连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码 System.out.println("Success connect Mysql server!"); Statement stmt = connect.createStatement(); ResultSet rs = stmt.executeQuery("select * from user"); //user 为你表的名称while (rs.next()) { System.out.println(rs.getString("name")); } } catch (Exception e) { System.out.print("get data error!"); e.printStackTrace(); } }}
点击运行程序:
Success loading Mysql Driver!
Success connect Mysql server!
huzhiheng
出现上面结果,说明你连接数据库成功。
import java.sql.*;publicclass Myjproject { publicstaticvoid main(String args[]) { try { Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序 //Class.forName("org.gjt.mm.mysql.Driver"); System.out.println("Success loading Mysql Driver!"); } catch (Exception e) { System.out.print("Error loading Mysql Driver!"); e.printStackTrace(); } try { Connection connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test","root","198876"); int num=100; PreparedStatement Statement=connect.prepareStatement("INSERT INTO user VALUES(?,?)"); for(int i=0;i<num;i++) //定义个100次的循环,往表里插入一百条信息。 { Statement.setString(1,"chongshi"+i); Statement.setString(2,"bo"+i); Statement.executeUpdate(); } // } catch (ClassNotFoundException e) { // TODO Auto-generated catch block // System.out.println("An error has occurred:"+e.toString()); // e.printStackTrace(); }catch(SQLException e) { } }}
5.下面我们打开MySQL数据库进行查看
mysql> show databases;
//查看所数据库
mysql> use test;
//使test为当前要操作的数据库
mysql> show tables;
//查看当前数据库的所有表
mysql> select *from user;
//查看当前表(user)的所有信息
注意:如果不能正常连接你的数据库,请检查你代码中,驱动、用户名、密码、表等信息是否对应无误,不要把别人的代码直接复制过来,看也不看就用。
本来不想写这么简单人文章,在百度上搜索我这个标题,完全符合标题的一大堆。但我按照那些文章捣鼓了很久,就是不行。
我的环境:MySQL:mysql-essential-5.1.51-win32
jdbc驱动:我已经上传到csdn上一个:http://download.csdn.net/source/3451945
Eclipse:任意版本,免费的,可以百度的到。
1。MySQL安装,不会的朋友可以看连接:http://www.duote.com/tech/1/2430_1.html
下面来创建一个数据:
mysql>CREATE DATABASE test; //创建一个数据库mysql>use test; //指定test为当前要操作的数据库mysql>CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); //创建一个表user,设置两个字段。mysql>INSERT INTO user VALUES('huzhiheng','123456'); //插入一条数据到表中
2。打开Eclipse,创建一个项目(my),
操作:右键点击my--->build Path--->add external Archiver...选择jdbc驱动,点击确定。
我的项目列表:
3。驱动已经导入,下面我们来写一个程序验证一下
import java.sql.*;publicclass MysqlJdbc { publicstaticvoid main(String args[]) { try { Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序 //Class.forName("org.gjt.mm.mysql.Driver"); System.out.println("Success loading Mysql Driver!"); } catch (Exception e) { System.out.print("Error loading Mysql Driver!"); e.printStackTrace(); } try { Connection connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test","root","198876"); //连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码 System.out.println("Success connect Mysql server!"); Statement stmt = connect.createStatement(); ResultSet rs = stmt.executeQuery("select * from user"); //user 为你表的名称while (rs.next()) { System.out.println(rs.getString("name")); } } catch (Exception e) { System.out.print("get data error!"); e.printStackTrace(); } }}
点击运行程序:
Success loading Mysql Driver!
Success connect Mysql server!
huzhiheng
出现上面结果,说明你连接数据库成功。
import java.sql.*;publicclass Myjproject { publicstaticvoid main(String args[]) { try { Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序 //Class.forName("org.gjt.mm.mysql.Driver"); System.out.println("Success loading Mysql Driver!"); } catch (Exception e) { System.out.print("Error loading Mysql Driver!"); e.printStackTrace(); } try { Connection connect = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test","root","198876"); int num=100; PreparedStatement Statement=connect.prepareStatement("INSERT INTO user VALUES(?,?)"); for(int i=0;i<num;i++) //定义个100次的循环,往表里插入一百条信息。 { Statement.setString(1,"chongshi"+i); Statement.setString(2,"bo"+i); Statement.executeUpdate(); } // } catch (ClassNotFoundException e) { // TODO Auto-generated catch block // System.out.println("An error has occurred:"+e.toString()); // e.printStackTrace(); }catch(SQLException e) { } }}
5.下面我们打开MySQL数据库进行查看
mysql> show databases;
//查看所数据库
mysql> use test;
//使test为当前要操作的数据库
mysql> show tables;
//查看当前数据库的所有表
mysql> select *from user;
//查看当前表(user)的所有信息
注意:如果不能正常连接你的数据库,请检查你代码中,驱动、用户名、密码、表等信息是否对应无误,不要把别人的代码直接复制过来,看也不看就用。
我的环境:MySQL:mysql-essential-5.1.51-win32
jdbc驱动:我已经上传到csdn上一个:http://download.csdn.net/source/3451945
Eclipse:任意版本,免费的,可以百度的到。
1。MySQL安装,不会的朋友可以看连接:http://www.duote.com/tech/1/2430_1.html
mysql>CREATE DATABASE test; //创建一个数据库mysql>use test; //指定test为当前要操作的数据库mysql>CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); //创建一个表user,设置两个字段。mysql>INSERT INTO user VALUES('huzhiheng','123456'); //插入一条数据到表中
2。打开Eclipse,创建一个项目(my),
操作:右键点击my--->buildPath--->add externalArchiver...选择jdbc驱动,点击确定。
我的项目列表:
3。驱动已经导入,下面我们来写一个程序验证一下
import java.sql.*;public class MysqlJdbc {public static void main(String args[]) {try {Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序 //Class.forName("org.gjt.mm.mysql.Driver");System.out.println("Success loading Mysql Driver!");}catch (Exception e) {System.out.print("Error loading Mysql Driver!");e.printStackTrace();}try {Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","198876");//连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码System.out.println("Success connect Mysql server!");Statement stmt = connect.createStatement();ResultSet rs = stmt.executeQuery("select * from user");//user 为你表的名称while (rs.next()) {System.out.println(rs.getString("name"));}}catch (Exception e) {System.out.print("get data error!");e.printStackTrace();}}}
点击运行程序:
Success loading Mysql Driver!
Success connect Mysql server!
huzhiheng
出现上面结果,说明你连接数据库成功。
import java.sql.*;public class Myjproject {public static void main(String args[]){try {Class.forName("com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序 //Class.forName("org.gjt.mm.mysql.Driver");System.out.println("Success loading Mysql Driver!");}catch (Exception e) {System.out.print("Error loading Mysql Driver!");e.printStackTrace();}try {Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","198876");int num=100;PreparedStatement Statement=connect.prepareStatement("INSERT INTO user VALUES(?,?)");for(int i=0;i<</SPAN>num;i++) //定义个100次的循环,往表里插入一百条信息。{Statement.setString(1,"chongshi"+i);Statement.setString(2,"bo"+i);Statement.executeUpdate();}// } catch (ClassNotFoundException e) {// TODO Auto-generated catch block// System.out.println("An error has occurred:"+e.toString());// e.printStackTrace();}catch(SQLException e){}}}
5.下面我们打开MySQL数据库进行查看
mysql> show tatabases;
//查看所数据库
mysql> use test;
//使test为当前要操作的数据库
mysql> show tables;
//查看当前数据库的所有表
mysql> select *from user;
//查看当前表(user)的所有信息
注意:如果不能正常连接你的数据库,请检查你代码中,驱动、用户名、密码、表等信息是否对应无误,不要把别人的代码直接复制过来,看也不看就用。
在eclipse中导入SQL驱动(jdbc驱动)方法还有其他:
1.你有很多种方法可以把这个jar包导入到工程的classpath,最简单的是加入jar包,project->properties->java
2.在Eclipse中导入mysql-connector-java-version.jar包(在项目名上右键,单击“属性”,再在选择Libraries,导入外部Jar即可)。
3........
- Eclipse连接MySQL数据库
- Eclipse连接MySQL数据库
- Eclipse连接MySQL数据库
- Eclipse连接MySQL数据库
- Eclipse连接MySQL数据库
- eclipse连接mysql数据库
- Eclipse连接MySQL数据库
- eclipse连接mysql数据库
- eclipse连接mysql数据库
- Eclipse连接MySQL数据库
- eclipse连接mysql数据库
- Eclipse连接MySQL数据库
- Eclipse连接MySQL数据库
- Eclipse连接MySQL数据库
- eclipse 连接 mysql 数据库!
- Eclipse 连接MySql数据库
- eclipse连接MySQL数据库
- eclipse连接MySQL数据库
- Java API—— Random类
- 空心菱形
- codevs3013 单词背诵
- [CodeForces519E]A and B and Lecture Rooms(lca)
- 不用后悔的ListView,GridView万能适配器 - 相同,不同 行布局都能用-下
- Eclipse连接MySQL数据库
- java ArrayList源代码实现
- MySQL中BLOB字段类型介绍
- (提高篇)第十五讲 项目 简化的银行储蓄系统(函数版)
- android动画的setAnimation与startAnimation的区别
- 最值
- 堆和栈的区别
- 洛谷 P1120 小木棍 [数据加强版]
- python中尝试pip install PIL,出现错误的解决方法