JDBC Connection MySQL

Source: Internet
Author: User

First step: Create a new data table:

------------------------------------------------------------------------------------------------------------
Use ESHOP1;
CREATE TABLE C_goods (
Ccode int PRIMARY KEY,
CNAME varchar (200)
);
Add data
Insert into C_goods (ccode,cname) VALUES (1000, ' book '), (1001, ' costume '), (1002, ' home '), (1003, ' digital ');
------------------------------------------------------------------------------------------------------------
Use ESHOP1;
CREATE TABLE P_goods (
Pcode int,
PName varchar (200),
Psorts int
);

Add data
Insert into P_goods (pcode,pname,psorts) VALUES (1, ' pages ', 1000), (2, ' author ', 1000), (3, ' brand ', 1001), (4, ' material ', 1002), (5, ' Battery life ', 1003);
--------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------

Step Two: Add

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------

Step three: JDBC Code:

Package day_3;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;

public class Showgoodnameandprice {

public static void Main (string[] args) {
TODO auto-generated Method Stub
/*
* 1. Load the driver because there may be multiple
* Drivers for database products (multiple jars)
*/
try {
Class.forName ("Com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
/**
* 2. Establishing a database connection
*
*/
String user = "root";
String password= "123456";
String url= "JDBC:MYSQL://LOCALHOST:3306/ESHOP1";
Connection Con=null;
try {
Con=drivermanager.getconnection (URL, user, password);
/***
* 3. Execute the SQL statement,
* Communication with Database server *statement interface object
*/
String sql= "Select *from c_goods C join P_goods p on c.ccode=p.psorts;";
Statement stmt=con.createstatement ();
/****
* 4. Get results, process business logic
*/
ResultSet rs=stmt.executequery (SQL);
while (Rs.next ()) {

String cname=rs.getstring ("cname");
Double ccode=rs.getdouble ("Ccode");

String pname = rs.getstring ("PName");
int pcode = Rs.getint ("Pcode");
int psorts = Rs.getint ("Psorts");

SYSTEM.OUT.PRINTLN ("Product List");
System.out.println ("No.:" +ccode+ "Product Type:" +cname);
SYSTEM.OUT.PRINTLN ("Commodity parameter list");
System.out.println ("Number:" +pcode+ "parameter name:" +pname+ "parameter belongs to the category:" + psorts);
System.out.println ();
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
Con.close ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

}

}

JDBC Connection MySQL

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.