JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » JDBC/SQL討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 新手學JDBC insert into 多欄位問題
李巧虎





發文: 6
積分: 0
於 2016-09-09 10:11 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
各位前輩好~ 小弟我 最近開始接觸Servlt&JSP 最近練習到JDBC,想請問各位前輩如何解決遇到類似下列這種問題:

要將多個欄位的資料新增到資料庫中如:insert into ac001 (AC001001,AC001002) values ('302','test')

我目前是有自己找到解決方法,想請問各位前輩是否還有更好的寫法呢?

謝謝!!

我自己的寫法:

前端部分index.html :

<!DOCTYPE html>

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="test">
客戶名稱<input type="text" name="client_name"/><br>
業務代表<input type="text" name="client_business"/><br>
手機<input type="text" name="client_phone"/><br>
公司電話<input type="text" name="client_phone2"/><br>
公司傳真<input type="text" name="client_fax"/><br>
公司地址<input type="text" name="client_address"/><br>
統編<input type="text" name="client_uniform"/><br>
建檔日期<input type="text" name="client_addtime"/><br>
備註<input type="text" name="client_remark"/><br>

<h1>第二個</h1>
客戶名稱<input type="text" name="client_name"/><br>
業務代表<input type="text" name="client_business"/><br>
手機<input type="text" name="client_phone"/><br>
公司電話<input type="text" name="client_phone2"/><br>
公司傳真<input type="text" name="client_fax"/><br>
公司地址<input type="text" name="client_address"/><br>
統編<input type="text" name="client_uniform"/><br>
建檔日期<input type="text" name="client_addtime"/><br>
備註<input type="text" name="client_remark"/><br>
<input type="submit" value="送出"/>
</form>
</body>
</html>

Control部分:

package control;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Model.TigerSQL;
public class test extends HttpServlet {
@Override
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{
//設定回復格式
response.setContentType("text/html; charset=UTF-8");
//設定回復工具
PrintWriter out = response.getWriter();
//取得Client參數
String field = "client_name,client_business,client_phone,client_phone2,client_fax,client_address,client_uniform,client_addtime,client_remark";
String[] client_name = request.getParameterValues("client_name");
String[] client_business = request.getParameterValues("client_business");
String[] client_phone = request.getParameterValues("client_phone");
String[] client_phone2 = request.getParameterValues("client_phone2");
String[] client_fax = request.getParameterValues("client_fax");
String[] client_address = request.getParameterValues("client_address");
String[] client_uniform = request.getParameterValues("client_uniform");
String[] client_addtime = request.getParameterValues("client_addtime");
String[] client_remark = request.getParameterValues("client_remark");

//將參數轉換為陣列
List sumData = new ArrayList();
for (int i = 0;i < client_name.length;i++){
sumData.add("'"+client_name[i] + "'," +
"'"+client_business[i] +"'," +
"'"+client_phone[i] +"'," +
"'"+client_phone2[i]+"'," +
"'"+client_fax[i]+"'," +
"'"+client_address[i]+"'," +
"'"+client_uniform[i]+"'," +
"'"+client_addtime[i]+"'," +
"'"+client_remark[i]+"'"
);
}
//建立資料,將資料傳送置資料庫
TigerSQL tsl = new TigerSQL("127.0.0.1", "帳號","密碼", "資料庫");
tsl.connect();
if(tsl.insertData("client", field, sumData)){
out.println("新增資料成功<br>");
}else{
System.out.println("新增資料錯誤");
}






}
}

Model部分:

package Model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class TigerSQL {
//設定基本資料
private String ip;
private String username;
private String password;
private String DB;
private Connection connection;
private String connurl;
private String conectdata;

//強制Client輸入基本資料
public TigerSQL(String ip , String username, String password,String DBlack Eye {
this.ip = ip ;
this.username = username;
this.password = password;
this.DB = DB;

//設定連線字串
connurl = "jdbc:mysql://"+ip+"/"+DB+"?useUnicode=true&characterEncoding=UTF-8";
}

public void connect() {
try {
//載入JDBC驅動
Class.forName("org.mariadb.jdbc.Driver");
System.err.println("載入驅動程式成功");

//連線資料庫
connection = DriverManager.getConnection(connurl,username,password);
//System.err.println("連線資料庫成功");
} catch (Exception e) {
//System.err.println("載入驅動程是失敗或者是連線資料庫不成功");
//System.err.println(connurl);
}
}

public void conectClose(){
try {
//設定資料庫連線關閉
if (connection != null){
connection.close();
//System.out.println("資料庫連線已關閉");
}
} catch (Exception e) {
// System.err.println("資料庫連線關閉發生錯誤" + e);
}
}
//insert方法
public boolean insertData(String datatable,String field,List data){
String insertSQL = null ;
boolean status = false;
try {
//取得連線
Statement stm = connection.createStatement();
//送出資料
for (int i = 0 ;i<data.size();i++){
insertSQL = "insert into " + datatable + " ("+field+") values (" + data.getLight Bulb + ") ";
stm.executeUpdate(insertSQL);
}
//關閉資料表
stm.close();
//關閉連線
connection.close();
status = true;
} catch (SQLException ex) {

//System.err.println(ex);
status = false;
}
return status;
}


}


reply to postreply to post
作者 Re:新手學JDBC insert into 多欄位問題 [Re:李巧虎]
Landgray





發文: 78
積分: 0
於 2016-09-10 03:40 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
有, 例如
...
業務代表<input type="text" name="vo[0].clientBusiness"/><br>

<h1>第二個</h1>
業務代表<input type="text" name="vo[1].clientBusiness"/><br>
...

學struts,spring等 都會學到這樣的寫法
後端在接時也是用List<VO>可以就接到
細節很多就不詳述


reply to postreply to post
作者 Re:新手學JDBC insert into 多欄位問題 [Re:李巧虎]
jamestian





發文: 250
積分: 0
於 2016-09-10 09:14 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
前端把資料轉為JsonObject 傳到後端,

後端把Json轉為POJO, 然後使用Dao去做基本的database operation.

學OO的精隨才是重點 .


reply to postreply to post
作者 Re:新手學JDBC insert into 多欄位問題 [Re:李巧虎]
李巧虎





發文: 6
積分: 0
於 2016-09-11 04:54 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感謝兩位前輩指導 我會一步一步慢慢學習XD

謝謝Big Smile


reply to postreply to post
» JWorld@TW »  JDBC/SQL討論區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8