The connection problem between MySQL and Eclipse

来源:互联网 发布:利用xampp进入mysql 编辑:程序博客网 时间:2024/05/05 01:59
1.Download and install "Eclipse"
   You can refer to:       http://blog.csdn.net/zxm490484080/article/details/52403307


2.Download and install "MySQL"
   You can refer to:       http://blog.csdn.net/sinat_34817187/article/details/51283595


3.Download "mysql-connector-java.jar"


4.

 4.1 cmd;

 4.2 Enter: mysql -u root -p     click Enter

 4.3 Enter password           click Enter

 4.4 :



5.Next, create a data

mysql>CREATE   DATABASE test;   //create a databasemysql>use  test;  //Specify test as the database to be operated onmysql>CREATE  TABLE  user (name VARCHAR(20),password VARCHAR(20));   //create a table  user,Set two fields.mysql>INSERT  INTO  user  VALUES('huzhiheng','123456'); //Insert a data into a table


6.Open Eclipse and create a project,

    Specific operations : right click my--->build Path--->Add external Archiver...select the "mysql-connector-java.jar"

                      

                      


7.The drive has been imported,let us write a program to verify it:

package com.nuist.One;import java.sql.*;public class ArrayOne {  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","599494.");           //连接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();    }  }}
   Running program:

Success loading Mysql Driver!Success connect Mysql server!LeiYi
The above results indicate that you have successfully connected the database


8.The following example inserts 200 data into the user table of MySQL

import java.sql.*;publicclass ArrayOne { 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","599494.");            int num=100;       PreparedStatement Statement=connect.prepareStatement("INSERT INTO user VALUES(?,?)");       for(int i=0;i

9.Next, we open the MySQL database for viewing

mysql> show databases; 
mysql> use  test;   
mysql> show tables;
mysql> select *from user;


OK!!


阅读全文
0 0
原创粉丝点击