JDBC串連mysql

來源:互聯網
上載者:User

標籤:package   載入   exe   sql   商品   final   資料庫   getc   建立   

第一步:建立資料表:

------------------------------------------------------------------------------------------------------------
use eshop1;
create table c_goods(
ccode int primary key,
cname varchar(200)
);
添加資料
insert into c_goods(ccode,cname) values(1000,‘圖書‘),(1001,‘服裝‘),(1002,‘家居‘),(1003,‘數位‘);
------------------------------------------------------------------------------------------------------------
use eshop1;
create table p_goods(
pcode int,
pname varchar(200),
psorts int
);

添加資料
insert into p_goods(pcode,pname,psorts) values(1,‘頁數‘,1000),(2,‘作者‘,1000),(3,‘品牌‘,1001),(4,‘材質‘,1002),(5,‘續航時間‘,1003);
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

第二步:添加

 

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

第三步:JDBC代碼:

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.載入驅動程式,因為應用當中,可能存在多個
* 資料庫產品的驅動程式(多個jar)
*/
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* 2.建立資料庫連接
*
*/
String user ="root";
String password="123456";
String url="jdbc:mysql://localhost:3306/eshop1";
Connection con=null;
try {
con=DriverManager.getConnection(url, user, password);
/***
* 3.執行sql語句,
* 與資料庫伺服器進行通訊 *Statement介面對象
*/
String sql=" select *from c_goods c join p_goods p on c.ccode=p.psorts;";
Statement stmt=con.createStatement();
/****
* 4.得到結果,處理商務邏輯
*/
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("商品列表");
System.out.println( "編號:" +ccode+" 商品類型:"+cname );
System.out.println( "商品參數表" );
System.out.println( "編號:" +pcode+" 參數名稱:"+pname+" 參數所屬分類:"+ 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串連mysql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.