當前位置:首頁 » 數據倉庫 » jdbc寫入資料庫
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

jdbc寫入資料庫

發布時間: 2022-07-04 10:38:46

Ⅰ JDBC訪問資料庫的步驟

載入(注冊)資料庫驅動(到JVM)。建立(獲取)資料庫連接。創建(獲取)資料庫操作對象。定義操作的sql語句。執行資料庫操作。獲取並操作結果集。

在資料庫的發展歷史上,資料庫先後經歷了層次資料庫、網狀資料庫和關系資料庫等各個階段的發展,資料庫技術在各個方面的快速的發展。特別是關系型資料庫已經成為目前資料庫產品中最重要的一員。

80年代以來, 幾乎所有的資料庫廠商新出的資料庫產品都支持關系型資料庫,即使一些非關系資料庫產品也幾乎都有支持關系資料庫的介面。這主要是傳統的關系型資料庫可以比較好的解決管理和存儲關系型數據的問題。

資料庫管理系統是為管理資料庫而設計的電腦軟體系統,一般具有存儲、截取、安全保障、備份等基礎功能。資料庫管理系統可以依據它所支持的資料庫模型來作分類,例如關系式、XML;或依據所支持的計算機類型來作分類,例如伺服器群集、行動電話。

或依據所用查詢語言來作分類,例如SQL、XQuery;或依據性能沖量重點來作分類,例如最大規模、最高運行速度;亦或其他的分類方式。不論使用哪種分類方式,一些DBMS能夠跨類別,例如,同時支持多種查詢語言。

Ⅱ jdbc連接資料庫的幾個步驟

創建一個以JDBC連接資料庫的程序,包含7個步驟:
1、載入JDBC驅動程序:
在連接資料庫之前,首先要載入想要連接的資料庫的驅動到JVM(Java虛擬機),
這通過java.lang.Class類的靜態方法forName(String className)實現。
例如:
try{
//載入MySql的驅動類
Class.forName("com.mysql.jdbc.Driver") ;
}catch(ClassNotFoundException e){
System.out.println("找不到驅動程序類 ,載入驅動失敗!");
e.printStackTrace() ;
}
成功載入後,會將Driver類的實例注冊到DriverManager類中。
2、提供JDBC連接的URL
•連接URL定義了連接資料庫時的協議、子協議、數據源標識。
•書寫形式:協議:子協議:數據源標識
協議:在JDBC中總是以jdbc開始
子協議:是橋連接的驅動程序或是資料庫管理系統名稱。
數據源標識:標記找到資料庫來源的地址與連接埠。
例如:(MySql的連接URL)
jdbc:mysql:
//localhost:3306/test?useUnicode=true&characterEncoding=gbk ;
useUnicode=true:表示使用Unicode字元集。如果characterEncoding設置為
gb2312或GBK,本參數必須設置為true 。characterEncoding=gbk:字元編碼方式。
3、創建資料庫的連接
•要連接資料庫,需要向java.sql.DriverManager請求並獲得Connection對象,
該對象就代表一個資料庫的連接。
•使用DriverManager的getConnectin(String url , String username ,
String password )方法傳入指定的欲連接的資料庫的路徑、資料庫的用戶名和
密碼來獲得。
例如:
//連接MySql資料庫,用戶名和密碼都是root
String url = "jdbc:mysql://localhost:3306/test" ;
String username = "root" ;
String password = "root" ;
try{
Connection con =
DriverManager.getConnection(url , username , password ) ;
}catch(SQLException se){
System.out.println("資料庫連接失敗!");
se.printStackTrace() ;
}
4、創建一個Statement
•要執行SQL語句,必須獲得java.sql.Statement實例,Statement實例分為以下3
種類型:
1、執行靜態SQL語句。通常通過Statement實例實現。
2、執行動態SQL語句。通常通過PreparedStatement實例實現。
3、執行資料庫存儲過程。通常通過CallableStatement實例實現。
具體的實現方式:
Statement stmt = con.createStatement() ;
PreparedStatement pstmt = con.prepareStatement(sql) ;
CallableStatement cstmt =
con.prepareCall("{CALL demoSp(? , ?)}") ;
5、執行SQL語句
Statement介面提供了三種執行SQL語句的方法:executeQuery 、executeUpdate
和execute
1、ResultSet executeQuery(String sqlString):執行查詢資料庫的SQL語句
,返回一個結果集(ResultSet)對象。
2、int executeUpdate(String sqlString):用於執行INSERT、UPDATE或
DELETE語句以及SQL DDL語句,如:CREATE TABLE和DROP TABLE等
3、execute(sqlString):用於執行返回多個結果集、多個更新計數或二者組合的
語句。
具體實現的代碼:
ResultSet rs = stmt.executeQuery("SELECT * FROM ...") ;
int rows = stmt.executeUpdate("INSERT INTO ...") ;
boolean flag = stmt.execute(String sql) ;
6、處理結果
兩種情況:
1、執行更新返回的是本次操作影響到的記錄數。
2、執行查詢返回的結果是一個ResultSet對象。
• ResultSet包含符合SQL語句中條件的所有行,並且它通過一套get方法提供了對這些
行中數據的訪問。
• 使用結果集(ResultSet)對象的訪問方法獲取數據:
while(rs.next()){
String name = rs.getString("name") ;
String pass = rs.getString(1) ; // 此方法比較高效
}
(列是從左到右編號的,並且從列1開始)
7、關閉JDBC對象
操作完成以後要把所有使用的JDBC對象全都關閉,以釋放JDBC資源,關閉順序和聲
明順序相反:
1、關閉記錄集
2、關閉聲明
3、關閉連接對象
if(rs != null){ // 關閉記錄集
try{
rs.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
if(stmt != null){ // 關閉聲明
try{
stmt.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
if(conn != null){ // 關閉連接對象
try{
conn.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}

Ⅲ 怎樣用JDBC把文本數據導入到Oracle資料庫中

1.讀取文本文件,每次讀取一行,用BufferedReader
2.因為每一行中都是固定的格式,因此解析每一行中的數據。
3.將解析的數據保存到資料庫。

BufferedReader br = new BufferedReader(new FileReader(new File("aa.txt")));
String temp = null;
// 假定這是你寫的將數據插入資料庫的介面和實現類。
Dao = new DaoImpl();

while ((temp = br.readLine()) != null) {
String[] strs = temp.split("|");
String s1 = strs[0];//如上面的2300
String s2 = strs[1]; // 如上面的62220202222
String s3 = strs[2];//如上面的2000
String s4 = strs[3]; // 如上面的村鎮銀行3
//還需要寫一個方法將數據插入資料庫。
.insert(s1,s2,s3,s4);
}
br.close();

請自行導入所需要的包,並處理異常。

Ⅳ java怎樣將讀取數據寫入資料庫

Java可以使用JDBC對資料庫進行讀寫。JDBC訪問一般分為如下流程:

一、載入JDBC驅動程序:
在連接資料庫之前,首先要載入想要連接的資料庫的驅動到JVM(Java虛擬機), 這通過java.lang.Class類的靜態方法forName(String className)實現。

例如:

try{

//載入MySql的驅動類

Class.forName("com.mysql.jdbc.Driver") ;

}catch(ClassNotFoundException e){

System.out.println("找不到驅動程序類 ,載入驅動失敗!");

e.printStackTrace() ;
}

成功載入後,會將Driver類的實例注冊到DriverManager類中。

二、提供JDBC連接的URL 連接URL定義了連接資料庫時的協議、子協議、數據源標識。

書寫形式:協議:子協議:數據源標識 協議:在JDBC中總是以jdbc開始

子協議:是橋連接的驅動程序或是資料庫管理系統名稱。

數據源標識:標記找到資料庫來源的地址與連接埠。

例如:(MySql的連接URL)

jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk

useUnicode=true:表示使用Unicode字元集。如果characterEncoding設置為

gb2312或GBK,本參數必須設置為true 。characterEncoding=gbk:字元編碼方式。

三、創建資料庫的連接

要連接資料庫,需要向java.sql.DriverManager請求並獲得Connection對象,該對象就代表一個資料庫的連接。

使用DriverManager的getConnectin(String url,String username,String password )方法傳入指定的欲連接的資料庫的路徑、資料庫的用戶名和密碼來獲得。

例如:
//連接MySql資料庫,用戶名和密碼都是root

String url = "jdbc:mysql://localhost:3306/test" ;

String username = "root" ;

String password = "root" ;

try{

Connection con =
DriverManager.getConnection(url , username , password ) ;
}catch(SQLException se){

System.out.println("資料庫連接失敗!");
se.printStackTrace() ;
}

四、創建一個Statement
要執行SQL語句,必須獲得java.sql.Statement實例,Statement實例分為以下3種類型:

1、執行靜態SQL語句。通常通過Statement實例實現。

2、執行動態SQL語句。通常通過PreparedStatement實例實現。

3、執行資料庫存儲過程。通常通過CallableStatement實例實現。

具體的實現方式:
Statement stmt = con.createStatement() ;

PreparedStatement pstmt = con.prepareStatement(sql) ;

CallableStatement cstmt = con.prepareCall("{CALL demoSp(? , ?)}") ;

五、執行SQL語句

Statement介面提供了三種執行SQL語句的方法:executeQuery 、executeUpdate和execute

1、ResultSet executeQuery(String sqlString):執行查詢資料庫的SQL語句,返回一個結果集(ResultSet)對象。

2、int executeUpdate(String sqlString):用於執行INSERT、UPDATE或DELETE語句以及SQL DDL語句,如:CREATE TABLE和DROP TABLE等

3、execute(sqlString):用於執行返回多個結果集、多個更新計數或二者組合的語句。
具體實現的代碼:

ResultSet rs = stmt.executeQuery("SELECT * FROM ...") ;

int rows = stmt.executeUpdate("INSERT INTO ...") ;

boolean flag = stmt.execute(String sql) ;

六、處理結果 兩種情況:
1、執行更新返回的是本次操作影響到的記錄數。

2、執行查詢返回的結果是一個ResultSet對象。

ResultSet包含符合SQL語句中條件的所有行,並且它通過一套get方法提供了對這些行中數據的訪問。

使用結果集(ResultSet)對象的訪問方法獲取數據:

while(rs.next()){

String name = rs.getString("name") ;

String pass = rs.getString(1); // 此方法比較高效(列是從左到右編號的,並且從列1開始)
}

七、關閉JDBC對象
操作完成以後要把所有使用的JDBC對象全都關閉,以釋放JDBC資源,關閉順序和聲明順序相反:

1、關閉記錄集

2、關閉聲明

3、關閉連接對象

if(rs != null){ // 關閉記錄集
try{
rs.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}

if(stmt != null){ // 關閉聲明
try{
stmt.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}

if(conn != null){ // 關閉連接對象
try{
conn.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}

(4)jdbc寫入資料庫擴展閱讀

樣例

package first;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.concurrent.Executors;

import java.util.concurrent.ScheledExecutorService;

import java.util.concurrent.TimeUnit;

public class lianjie {

public static void main(String[] args) {

Runnable runnable = new Runnable() {

public void run() {

//聲明Connection對象

Connection con;

//驅動程序名

String driver1 = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

//URL指向要訪問的資料庫名

String url1 = "jdbc:sqlserver://IP地址和埠號;DateBaseName=資料庫名";

//MySQL配置時的用戶名

String user1 = "user";

//MySQL配置時的密碼

String password1 = "user";

//聲明Connection對象

Connection con1;

//驅動程序名

String driver2 = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

//URL指向要訪問的資料庫名

String url2 = "jdbc:sqlserver://IP地址和埠號;DateBaseName=資料庫名";

//MySQL配置時的用戶名

String user2 = "user";

//MySQL配置時的密碼

String password2 = "user";

//遍歷查詢結果集

try {

//載入驅動程序

Class.forName(driver1);

//1.getConnection()方法,連接MySQL資料庫!!

con = DriverManager.getConnection(url1,user1,password1);

if(!con.isClosed())

System.out.println("成功連接到資料庫!");

try {

//載入驅動程序

Class.forName(driver2);

//1.getConnection()方法,連接MySQL資料庫!!

con1 = DriverManager.getConnection(url2,user2,password2);

if(!con1.isClosed())

System.out.println("成功連接到資料庫!");

//2.創建statement類對象,用來執行SQL語句!!

Statement statement = con.createStatement();

//要執行的SQL語句

String sql = "use 資料庫名 select * from 表名";

//3.ResultSet類,用來存放獲取的結果集!!

ResultSet rs = statement.executeQuery(sql);

//要執行的SQL語句

String sql1 = "use tiantiana insert into Table_1(tiantian,qiqi,yuyu)VALUES(?,?,?)";

//3.ResultSet類,用來存放獲取的結果集!!

PreparedStatement pst = con1.prepareStatement(sql1);

System.out.println ("tiantian"+"/t"+"qiqi"+"/t"+"yuyu");

while(rs.next()){

System.out.print(rs.getString(1));

System.out.print(rs.getString(2));

System.out.print(rs.getString(3));

pst.setString(1,rs.getString(1));

pst.setString(2,rs.getString(2));

pst.setString(3,rs.getString(3));

pst.executeUpdate();

}

pst.close();

rs.close();

//2.創建statement類對象,用來執行SQL語句!!

Statement statement1 = con.createStatement();

//要執行的SQL語句

String sql2 = "use 資料庫名 select * from 表名";

//3.ResultSet類,用來存放獲取的結果集!!

ResultSet rs1 = statement1.executeQuery(sql2);

//要執行的SQL語句

String sql3 = "use tiantiana insert into Table_2(tiantian1,qiqi1,yuyu1)VALUES(?,?,?)";

//3.ResultSet類,用來存放獲取的結果集!!

PreparedStatement pst1 = con1.prepareStatement(sql3);

System.out.println ("tiantian1"+"/t"+"qiqi1"+"/t"+"yuyu1");

while(rs1.next()){

System.out.print(rs1.getString(1));

System.out.print(rs1.getString(2));

System.out.print(rs1.getString(3));

pst1.setString(1,rs1.getString(1));

pst1.setString(2,rs1.getString(2));

pst1.setString(3,rs1.getString(3));

pst1.executeUpdate();

}

//關閉鏈接

rs1.close();

pst.close();

con1.close();

con.close();

} catch(ClassNotFoundException e) {

//資料庫驅動類異常處理

System.out.println("對不起,找不到驅動程序!");

e.printStackTrace();

} catch(SQLException e) {

//資料庫連接失敗異常處理

e.printStackTrace();

}catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}finally{

System.out.println("資料庫數據成功獲取!!");

}

} catch(ClassNotFoundException e) {

//資料庫驅動類異常處理

System.out.println("對不起,找不到驅動程序!");

e.printStackTrace();

} catch(SQLException e) {

//資料庫連接失敗異常處理

e.printStackTrace();

}catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}finally{

System.out.println("資料庫數據成功獲取!!");

}

}

};

ScheledExecutorService service = Executors

.();

// 第二個參數為首次執行的延時時間,第三個參數為定時執行的間隔時間

service.scheleAtFixedRate(runnable, 10, 60*2, TimeUnit.SECONDS);

}

}

Ⅳ JDBC操作資料庫如何快速學會

JDBC操作資料庫其實只要掌握了六大步驟就基本差不多啦:
1.載入驅動
Class.forName(String);
String(驅動器的名稱,分oracle,sql
server等資料庫驅動)
2.創建連接
DriverManager.getConnection(url,userName,passwd);
url是資料庫的地址,後面分別是用戶名和密碼
3.創建會話statement(三種statement)
stmt(Statement):所有stmt的父類從connection對象獲得,主要用於解析執行sql語句,返回響應結果,多執行異構的sql語句
pstmt(preparedStatement):主要用於執行同構的sql語句,stmt的子類。
cstmt:主要用於執行plsql的編程對象
4.執行sql語句
executeQuery();
5.處理結果集(有結果集返回,無則可省去,比如插入和刪除)
ResultSet
rs
=
步驟4的結果
6.釋放資源即連接,一般些在finally語句塊中,目的是減輕伺服器的壓力

Ⅵ jsp jdbc向資料庫寫入值

這是表單頁面代碼!

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,bean.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>圖書管理</title>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
</script>
<script language="javascript" src="tupian.js"></script>
<link href="css.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style1 {color: #FF0000}
.style2 {color: #0000FF}
-->
</style>
</head>

<body topmargin="0">

<%
if(session.getAttribute("admin")==null){
response.sendRedirect("adminlogin.jsp");
}
else{
%>
<div align="center">
<table width="800" height="530" border="0">
<tr>
<td height="48" colspan="3" align="left" valign="top"><jsp:include page="top.jsp" flush="true"></jsp:include></td>
</tr>
<tr>
<td width="196" height="220" align="left" valign="top"><div align="center" class="style1"><jsp:include page="adminleft.htm"></jsp:include>

</div></td>
<td width="363" align="left" valign="top"><div align="left" class="style1">圖書添加</div>
<form action="aabook.jsp" method="post" enctype="multipart/form-data" name="form2" onSubmit="return jjian()">
<p>書 名:
<input name="book_name" type="text" id="book_name">
</p>
<p>作 者:
<input name="author" type="text" id="author">
</p>
<p>出版社:
<input name="publisher" type="text" id="publisher">
</p>
<p>類 型:
<input name="type" type="text" id="type">
</p>
<p>Ifnew: <span class="style1"> </span>
<input type="radio" name="ifNew" value="yes">
<input name="ifNew" type="radio" value="top" checked>
</p>
<p>價 格:
<input name="price" type="text" id="price">
</p>
<p>圖 片:
<input name="img" type="file" id="img" onChange="tupian(this)">
</p>
<p>簡 介:
<textarea name="jiejian" cols="50" rows="4" id="jiejian"></textarea>
</p>
<p>
<input type="submit" name="Submit" value="修改">

<input type="reset" name="Submit" value="重置">
</p>
</form>
<br> </td>
<td width="251" align="left" valign="top"><p></p>
<p></p>
<p></p>
<p><img name="rr" width="137" height="141" alt="圖片預覽"></p></td>
</tr>

<tr>
<td height="53" colspan="3" align="left" valign="top"><jsp:include page="end.jsp" flush="true"></jsp:include></td>
</tr>
</table>
<%}%>
</div>
</body>
</html>

這是接受頁面代碼

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,bean.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>無標題文檔</title>
</head>

<body>
<%
//String bookid=request.getParameter("book_id");
//int book_id=Integer.parseInt(bookid);
String book_name=request.getAttribute("book_name").toString();
//book_name=new String(book_name.getBytes("ISO-8859-1"),"GB2312");
String author=request.getAttribute("author").toString();
//author=new String(author.getBytes("ISO-8859-1"),"GB2312");
String publisher=request.getAttribute("publisher").toString();
//publisher=new String(publisher.getBytes("ISO-8859-1"),"GB2312");
String type=request.getAttribute("type").toString();
//type=new String(type.getBytes("ISO-8859-1"),"GB2312");
String ifNew=request.getAttribute("ifNew").toString();
String price=request.getAttribute("price").toString();
String img=request.getAttribute("fname").toString();
String jiejian=request.getAttribute("jiejian").toString();
//jiejian=new String(jiejian.getBytes("ISO-8859-1"),"GB2312");
String str[]={book_name,author,publisher,type,ifNew,price,img,jiejian};
String sql="insert into book values(?,?,?,?,?,?,?,?,getDate())";
DBMain.pexeUpdate(sql,str);
out.print("<a href=\"admin.jsp\">添加成功返回首頁</a>");
%>

</body>
</html>

這是資料庫處理類

package bean;
import java.sql.*;

public class DBMain {

//連接資料庫
public static Connection getcon(){
Connection con=null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
try {
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databaseName=bookshop","sa","2601350");
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
return con;
}

//用Statement查詢資料庫
public static ResultSet exeQuery(String sql){
ResultSet rs=null;
Connection con=getcon(); //Connection的對象
try {
Statement stm=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stm.executeQuery(sql);
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
return rs;

}

//用Statement類,執行添加,刪除,更新資料庫操作
public static void exeUpdate(String sql){
Connection con=getcon();
try {
Statement stm=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
stm.executeUpdate(sql);
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}

//用PreparedStatement類查詢資料庫
public static ResultSet pexeQuery(String sql,String[] str){
ResultSet rs=null;
Connection con=getcon();
try {
PreparedStatement pstm=con.prepareStatement(sql);
for(int i=0;i<str.length;i++){
pstm.setString(i+1,str[i]);
}
rs=pstm.executeQuery();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
return rs;
}

//用PreparedStatement類實現添加,刪除,更新資料庫
public static void pexeUpdate(String sql,String[] str){
try {
Connection con=getcon();
PreparedStatement pstm=con.prepareStatement(sql);
for(int i=0;i<str.length;i++){
pstm.setString(i + 1,str[i]);
}
pstm.executeUpdate();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}

}

這是我第一次連接資料庫寫的代碼,現在不適用,不過你可以感覺下,

Ⅶ jdbc添加記錄到資料庫

數組下表越界
stmt.setInt(1, num);
stmt.setString(2, name);
stmt.setInt(3, age);
stmt.setString(4, sex);
stmt.setString(5, college);
,沒有5個,結果你set了5個,肯定報數組下標越界

Ⅷ Java jdbc寫入資料庫報錯

FileReader fr1=new FileReader(filePath);
BufferedReader br1=new BufferedReader(fr1);
String line2=null;
try {
while(true){
//讀取txt裡面的每一行數據
line2=br1.readLine();
System.out.println(line2);

if(line2!=null)
{
//分割提取每一列數據
StringTokenizer dt=new StringTokenizer(line2,",");
Date=dt.nextToken();
OPrice=dt.nextToken();
HPrice=dt.nextToken();
LPrice=dt.nextToken();
CPrice=dt.nextToken();
Voume=dt.nextToken();
CRate=dt.nextToken();
System.out.println(Date);
System.out.println(OPrice);

//向資料庫的StockData表寫入數據
String sql;
sql="insert into StockData values ('"+line0+"','"+str[1]+"','"+Date+"','"+OPrice+"','"+HPrice+"','"+LPrice+"','"+CPrice+"','"+Voume+"','"+CRate+"')";
stmt.executeUpdate(sql);
}
else break;
}

} catch(Exception e) {
} finally {
if (con != null) {
con.close();
}
}

另外,對於字元串的多次拼接,盡量使用StringBuffer(這兒沒有給你修改出來)

Ⅸ JDBC連接資料庫的步驟都有哪些

1、首先我們通過資料庫可視化工具navicate for mysql,新建一個資料庫,名字叫test新建一張表。