Fools use Eclipse to connect to the MySQL database

  • 2020-11-18 06:30:36
  • OfStack

Originally do not want to write so simple person article, in Baidu search I this title, in full accord with the title of 1 heap. But I've been working on those articles for a long time, and it just doesn't work.

My environment: MySQL: ES4en-ES5en-5.1.51-win32

jdbc drive

Eclipse: Any version, free, you can baidu to.

MySQL installation, no friends can see the graphic interpretation https here: / / www ofstack. com article / 23876. htm, very detailed.

Let's create 1 data:


mysql>CREATE DATABASE test; // create 1 A database  
mysql>use test; // The specified test Is the database you are currently operating on  
mysql>CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); // create 1 A table user , set two fields.  
mysql>INSERT INTO user VALUES('huzhiheng','123456'); // insert 1 Bar data to the table  

2. Open Eclipse and create a project (my).

Operation: right click my-- > build Path--- > add external Archiver... Select the jdbc driver and click OK.

3. The driver has been imported, let's write a program to verify 1


import java.sql.*; 
public class MysqlJdbc { 
 public static void main(String args[]) { 
 try { 
  Class.forName("com.mysql.jdbc.Driver");  // loading MYSQL JDBC The driver  
  //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"); 
   // The connection URL for  jdbc:mysql// Server address / The database name   Behind, 2 The parameters are the login user name and password  
 
  System.out.println("Success connect Mysql server!"); 
  Statement stmt = connect.createStatement(); 
  ResultSet rs = stmt.executeQuery("select * from user"); 
                //user  Name of your table  
  while (rs.next()) { 
  System.out.println(rs.getString("name")); 
  } 
 } 
 catch (Exception e) { 
  System.out.print("get data error!"); 
  e.printStackTrace(); 
 } 
 } 
} 


Click to run the program:


Success loading Mysql Driver! 
Success connect Mysql server! 
huzhiheng


The above results indicate that you have successfully connected to the database.

4. If you can see the contents of MySQL, do we want to insert data into MySQL?

In the following example, insert 100 pieces of data into MySQL's user table


import java.sql.*; 
 
public class Myjproject { 
 public static void main(String args[]) 
 { 
  try { 
   Class.forName("com.mysql.jdbc.Driver");  // loading MYSQL JDBC The driver  
   //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++)  // To define a 100 Second loop, insert into the table 1 A hundred messages.  
  { 
   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. Now let's open the MySQL database for a view


mysql> show tatabases; // View all databases  
mysql> use test; // make test Is the database you are currently operating on  
mysql> show tables; // View all the tables in the current database  
view sourceprint? 
mysql> select *from user; // View the current table ( user )  

Note: if the normal connection to your database, please check your code, the driver, user name, password, table and other information is correct, do not copy other people's code directly over, do not look at the use.


Related articles: