"Database" Eclipse connects to MySQL database

Source: Internet
Author: User

My environment: Mysql:mysql-essential-5.1.51-win32

JDBC Driver: I've uploaded it to csdn. Previous: http://download.csdn.net/detail/paulwinflo/8818921

Eclipse: Any version, free, can Baidu's to.

1. here's how to create a data:

Mysql>CREATEDATABASETest//Create a database

Mysql>UseTest//Specify test as the current database to be manipulated

Mysql>CREATETABLEUser(NameVARCHAR(20), password varchar20// Create a table user, set two fields.

Mysql>insert into user values ( ' paulwinflo ", ' 123456 "); // insert a data into the table

  

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

Action: Right-click my--->build Path--->add external archiver ... Select the JDBC driver and click OK.

List of my projects:

3. Driver has been imported, let's write a program to verify

Importjava.sql.*;
PublicClassMYSQLJDBC {
PublicStaticvoidMain (String args[]) {
Try{
Class.forName ("Com.mysql.jdbc.Driver");//Load MySQL JDBC 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");
//Connection URL is Jdbc:mysql//Server address/database name, the following 2 parameters are login username and password, respectively

System.out.println ("Success Connect Mysql server!");
Statement stmt=Connect.createstatement ();
ResultSet RS= Stmt.executequery (" SELECT * from User " //
Span style= "color: #0000ff;" >while (Rs.next ()) {
System.out.println (rs.getstring ( "name" ));
}
}
catch (Exception e) {
System.out.print ( "get data Error! ");
E.printstacktrace ();
}
}
} /span>

Click to run the program:

Success Loading Mysql driver!

Success Connect Mysql server!

Huzhiheng

The results above show that you are connected to the database successfully.

4. Can see the contents of MySQL, then we want to insert data into MySQL. In the following example, insert 100 data into the user table of MySQL
Importjava.sql.*;

PublicClassMyjproject {
PublicStaticvoidMain (String args[])
{
Try{
Class.forName ("Com.mysql.jdbc.Driver");//Load MySQL JDBC 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");

IntNum=100;
PreparedStatement Statement=Connect.preparestatement ("INSERT into User VALUES (?,?)");
For(IntI=0; I<Num;i++)//Define a loop of 100 times and insert 100 messages into the table.
{
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 had occurred:" +e.tostring ());
// E.printstacktrace ();
}catch ( SQLException e)
{
}
}

 

5. Below we open the MySQL database for viewing

mysql> show databases;  //查看所数据库
mysql> use  test;    //使test为当前要操作的数据库
mysql> show tables; //查看当前数据库的所有表
mysql> select *from user;  //查看当前表(user)的所有信息

"Database" Eclipse connects to MySQL database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.