MyEclipse through JDBC Connection Mysql database Basic Introduction _mysql

Source: Internet
Author: User
Tags getmessage zip
1. The premise is that MyEclipse has been able to properly develop Java engineering
2. Install MySQL
The individual is using the version is Mysql-5.0.22-win32.zip
URL: http://www.mysql.com/downloads/mysql/#downloads
3. Download JDBC Driver
The individual is using mysql-connector-java-5.1.22.zip, what is needed is the Mysql-connector-java-5.1.22-bin.jar after the decompression
URL: http://www.mysql.com/downloads/connector/j/
4. Code Testing
Copy Code code as follows:

Package ts.jsj.lyh;

Import java.sql.*;

/** *//**
* Process of using JDBC to connect to database MySQL
* DATABASE:JSJ, Table:student;
* @author Duchangfeng 2008 09 18
*/
public class Jdbctest {

public static Connection getconnection () throws SQLException,
Java.lang.ClassNotFoundException
{
The first step: to load MySQL's JDBC driver
Class.forName ("Com.mysql.jdbc.Driver");

Get the URL of the connection, access to the MySQL database username, password; JSJ: Database name
String url = "JDBC:MYSQL://LOCALHOST:3306/JSJ";
String username = "root";
String Password = "111";

Step Two: Create an instance of the connection class with the MySQL database
Connection con = drivermanager.getconnection (URL, username, password);
return con;
}


public static void Main (String args[]) {
Try
{
Step three: Get connection class instance con, use Con to create Statement object class instance Sql_statement
Connection con = getconnection ();
Statement sql_statement = Con.createstatement ();

/** *//************ to the database for related operations ************/
If a database exists with the same name, delete
Sql_statement.executeupdate ("drop table if exists student");
An SQL statement was executed to generate a table named student
Sql_statement.executeupdate ("CREATE TABLE student (ID int not NULL auto_increment, name varchar NOT null default ' Na") Me ', math int not NULL default, primary key (ID)); ");
Inserting data into a table
Sql_statement.executeupdate ("Insert student values (1, ' liying ', 98)");
Sql_statement.executeupdate ("Insert student values (2, ' Jiangshan ', 88)");
Sql_statement.executeupdate ("Insert student values (3, ' Wangjiawu ', 78)");
Sql_statement.executeupdate ("Insert student values (4, ' Duchangfeng ', 100)");
---the above is not practical, but listed as a reference---

Fourth step: Execute the query, use the object of the ResultSet class, return the result of the query
String query = "SELECT * from student";
ResultSet result = sql_statement.executequery (query);
/** *//************ to the database for related operations ************/

SYSTEM.OUT.PRINTLN ("The data in the student table is as follows:");
System.out.println ("------------------------");
System.out.println ("School Number" + "" + "name" + "" + "Data Score");
System.out.println ("------------------------");

Processing the resulting query results and manipulating the object of the result class
while (Result.next ())
{
int number = Result.getint ("Sno");
String name = result.getstring ("sname");
String Mathscore = result.getstring ("Sgrade");
Get the data in the database
System.out.println ("" + number + "" + name + "" + Mathscore);
}

Close connections and declarations
Sql_statement.close ();
Con.close ();

catch (Java.lang.ClassNotFoundException e) {
Load JDBC Error, the driver you want to use is not found
System.err.print ("ClassNotFoundException");
Other errors
System.err.println (E.getmessage ());
catch (SQLException ex) {
Display database connection errors or query errors
System.err.println ("SQLException:" + ex.getmessage ());
}
}

}

Most of the above content from the network, thanks to Ape Ape's selfless dedication ~ ~ Concrete steps, powerful Internet are easier to query, here no longer repeat, now add a few points of personal thought need to pay attention to places:

1 about the location of the storage of Mysql-connector-java-5.1.22-bin.jar. Create a new folder for Jar packs (such as Lib) in a MyEclipse specific Java project, copy Mysql-connector-java-5.1.22-bin.jar to a folder, and select the Jar pack right-click--->build Path--->add to build Path.

If it appears

ClassNotFoundExceptioncom.mysql.jdbc.Driver

, it is due to the lack of import jar packs.

2 If you are familiar with the use of MySQL, you can ignore this one. When you test a connection, an individual always appears with this exception prompt:

Sqlexception:communications link Failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

This is because the user is not familiar with MySQL, a lot of trial and error to MySQL operation, I do not know when inadvertently MySQL services (if the installation of MySQL did not change, the default service name is MySQL) closed, the solution to open this service can be. Control Panel---> Management tools---> Services--->mysql---> select Enable.

3 when using the above code test, the place to change is:
User name, password, database name of MySQL database
Copy Code code as follows:

String url = "JDBC:MYSQL://LOCALHOST:3306/JSJ";
String username = "root";
String Password = "111";

and the name of the field to query in the specific base table:
Copy Code code as follows:

int number = Result.getint ("Sno");
String name = result.getstring ("sname");
String Mathscore = result.getstring ("Sgrade");

Have a lot to share, have questions welcome Exchange ~ ~
Related Article

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.