Java程序向數(shù)據(jù)庫(kù)中插入數(shù)據(jù),代碼如下:
創(chuàng)新互聯(lián)專(zhuān)注于企業(yè)成都全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站重做改版、云夢(mèng)網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁(yè)面制作、商城網(wǎng)站制作、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為云夢(mèng)等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
//首先創(chuàng)建數(shù)據(jù)庫(kù),(access,oracle,mysql,sqlsever)其中之一,其中access,sqlsever需要配置數(shù)據(jù)源(odbc);
//然后再eclipse中創(chuàng)建類(lèi)(ConnDb,Test,TestBean)ConnDb功能為連接數(shù)據(jù)庫(kù),查詢,插入,刪除,修改數(shù)據(jù)的類(lèi),Test為含有main方法的測(cè)試類(lèi),TestBean為數(shù)據(jù)表中的字段屬性及set,get方法
//以下是ConnDb代碼:
package?db;
import?java.sql.Connection;
import?java.sql.DriverManager;
import?
java.sql.ResultSet;
import?java.sql.SQLException;
import?
java.sql.Statement;
import?java.util.ArrayList;
public?class?ConnDb?{
public?Connection?startConn(Connection?conn){
try?{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn?=?DriverManager.getConnection("jdbc:odbc:數(shù)據(jù)庫(kù)","用戶名",?"密碼");
}?catch?(Exception?e)?{
System.out.println("連接數(shù)據(jù)庫(kù)時(shí)出現(xiàn)錯(cuò)誤");
}
return?conn;
}
public?ArrayList?executeQuery(String?sql){
Connection?conn?=?null;
Statement?stmt?=?null;
ResultSet?rs?=?null;
ArrayList?list?=?new?ArrayList();
try?{
conn?=?startConn(conn);
stmt?=?conn.createStatement();
rs?=?stmt.executeQuery(sql);//sql為a?href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLuARzuAw9mW-BuW0snHF-0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPHTzPHbkPWf3"?target="_blank"?class="baidu-highlight"sql語(yǔ)句/a例如"select?*?from?
表名",從main方法中傳進(jìn)來(lái),這里用的是ArrayList?類(lèi)將查詢結(jié)果存儲(chǔ)起來(lái)
while(rs.next()){
TestBean?tb?=?new?TestBean();
tb.setTid(rs.getString("tid"));
tb.setTname(rs.getString("tname"));
tb.setTinfo(rs.getString("tinfo"));
list.add(tb);
}
}?
catch?(SQLException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}finally{
closeConn(rs,stmt,conn);
}
return?list;
}?
public?void?executeUpdate(String?sql){
Connection?conn?=?null;
Statement?stmt?=?null;
try?{
conn?=?
startConn(conn);
stmt?=?conn.createStatement();
stmt.executeUpdate(sql);
}?
catch?(SQLException?e)?{
System.out.println("修改,插入或者刪除數(shù)據(jù)庫(kù)數(shù)據(jù)時(shí)發(fā)生錯(cuò)誤!");
}finally{
closeConn(stmt,conn);
}
}
public?void?closeConn(ResultSet?rs,Statement?stmt,Connection?conn){
try?{
if(rs?!=?
null){
rs.close();
}
if(stmt?!=?null){
stmt.close();
}
if(conn?!=?null){
conn.close();
}
}?
catch?(SQLException?e)?{
//?TODO?Auto-generated?catch?
block
System.out.println("關(guān)閉數(shù)據(jù)庫(kù)的時(shí)候發(fā)生錯(cuò)誤!");
}
}
public?void?closeConn(Statement?stmt,Connection?conn){
try?{
if(stmt?!=?null){
stmt.close();
}
if(conn?!=?null){
conn.close();
}
}?
catch?(SQLException?e)?{
//?TODO?Auto-generated?catch?block
System.out.println("關(guān)閉數(shù)據(jù)庫(kù)的時(shí)候發(fā)生錯(cuò)誤!");
}
}
}
寫(xiě)Java程序時(shí)經(jīng)常碰到要讀如txt或?qū)懭雝xt文件的情況,但是由于要定義好多變量,經(jīng)常記不住,每次都要查,特此整理一下,簡(jiǎn)單易用,方便好懂!
[java]?view?plain?copy
package?edu.thu.keyword.test;??
import?java.io.File;??
import?java.io.InputStreamReader;??
import?java.io.BufferedReader;??
import?java.io.BufferedWriter;??
import?java.io.FileInputStream;??
import?java.io.FileWriter;??
public?class?cin_txt?{??
static?void?main(String?args[])?{??
try?{?//?防止文件建立或讀取失敗,用catch捕捉錯(cuò)誤并打印,也可以throw??
/*?讀入TXT文件?*/??
String?pathname?=?"D:\\twitter\\13_9_6\\dataset\\en\\input.txt";?//?絕對(duì)路徑或相對(duì)路徑都可以,這里是絕對(duì)路徑,寫(xiě)入文件時(shí)演示相對(duì)路徑??
File?filename?=?new?File(pathname);?//?要讀取以上路徑的input。txt文件??
InputStreamReader?reader?=?new?InputStreamReader(??
new?FileInputStream(filename));?//?建立一個(gè)輸入流對(duì)象reader??
BufferedReader?br?=?new?BufferedReader(reader);?//?建立一個(gè)對(duì)象,它把文件內(nèi)容轉(zhuǎn)成計(jì)算機(jī)能讀懂的語(yǔ)言??
String?line?=?"";??
line?=?br.readLine();??
while?(line?!=?null)?{??
line?=?br.readLine();?//?一次讀入一行數(shù)據(jù)??
}??
/*?寫(xiě)入Txt文件?*/??
File?writename?=?new?File(".\\result\\en\\output.txt");?//?相對(duì)路徑,如果沒(méi)有則要建立一個(gè)新的output。txt文件??
writename.createNewFile();?//?創(chuàng)建新文件??
BufferedWriter?out?=?new?BufferedWriter(new?FileWriter(writename));??
out.write("我會(huì)寫(xiě)入文件啦\r\n");?//?\r\n即為換行??
out.flush();?//?把緩存區(qū)內(nèi)容壓入文件??
out.close();?//?最后記得關(guān)閉文件??
}?catch?(Exception?e)?{??
e.printStackTrace();??
}??
}??
}
就要鏈接數(shù)據(jù)庫(kù),可以通過(guò)JDBC鏈接。
首先,在連接數(shù)據(jù)庫(kù)之前必須保證SQL Server 2012是采用SQL Server身份驗(yàn)證方式而不是windows身份驗(yàn)證方式,開(kāi)始如下配置:
一、因?yàn)镾QL Server 2012裝好后,默認(rèn)協(xié)議是沒(méi)有開(kāi)啟的,所以要打開(kāi)SQL Server配置管理器中開(kāi)啟。
1、安裝好SQL Server 2012后,運(yùn)行 開(kāi)始 → 所有程序 → Microsoft SQL Server 2012 → 配置工具 →SQL Server配置管理器
2、在左邊欄找到 SQL Server網(wǎng)絡(luò)配置選項(xiàng),點(diǎn)開(kāi)它的小箭頭,會(huì)看到“【你的數(shù)據(jù)庫(kù)名】的協(xié)議” (圖中是ERIC2012的協(xié)議),選中它,看右邊欄。
(1)如果Named Pipes 未啟用,則右鍵→啟用
(2)右鍵單擊 TCP/IP,選擇 啟用
(3)雙擊TCP/IP(右鍵→屬性),在彈出的窗口中選擇 “IP地址” 選項(xiàng)卡,將IP1和IP10的【IP地址】設(shè)為127.0.0.1,并將所有【IPx】的【已啟用】設(shè)為是。接著,拖動(dòng)下拉條到最下方,將 IPAll 中的【TCP端口】設(shè)成 【1433】,其余不變。
3、重新啟動(dòng)計(jì)算機(jī)。
4、接下來(lái)使用telnet命令測(cè)試1433端口是否打開(kāi)。首先要保證telnet服務(wù)開(kāi)啟。
5、完成上一步后。開(kāi)始菜單 → 運(yùn)行cmd → 輸入:telnet 127.0.0.1 1433,(注意telnet與127之間有空格,1與1433之間有空格)。
6、若提示“不能打開(kāi)到主機(jī)的連接,在端口 1433: 連接失敗”,則說(shuō)明1433端口沒(méi)有打開(kāi),需要重新進(jìn)行以上配置。
Java程序向數(shù)據(jù)庫(kù)中插入數(shù)據(jù),代碼如下:
//首先創(chuàng)建數(shù)據(jù)庫(kù),(access,oracle,mysql,sqlsever)其中之一,其中access,sqlsever需要配置數(shù)據(jù)源(odbc);//然后再eclipse中創(chuàng)建類(lèi)(ConnDb,Test,TestBean)ConnDb功能為連接數(shù)據(jù)庫(kù),查詢,插入,刪除,修改數(shù)據(jù)的類(lèi),Test為含有main方法的測(cè)試類(lèi),TestBean為數(shù)據(jù)表中的字段屬性及set,get方法//以下是ConnDb代碼:package db;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;public class ConnDb {public Connection startConn(Connection conn){ try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection("jdbc:odbc:數(shù)據(jù)庫(kù)","用戶名", "密碼"); } catch (Exception e) { System.out.println("連接數(shù)據(jù)庫(kù)時(shí)出現(xiàn)錯(cuò)誤"); } return conn; } public ArrayList executeQuery(String sql){ Connection conn = null; Statement stmt = null; ResultSet rs = null; ArrayList list = new ArrayList(); try { conn = startConn(conn); stmt = conn.createStatement(); rs = stmt.executeQuery(sql);//sql為sql語(yǔ)句例如"select * from 表名",從main方法中傳進(jìn)來(lái),這里用的是ArrayList 類(lèi)將查詢結(jié)果存儲(chǔ)起來(lái) while(rs.next()){ TestBean tb = new TestBean(); tb.setTid(rs.getString("tid")); tb.setTname(rs.getString("tname")); tb.setTinfo(rs.getString("tinfo")); list.add(tb); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ closeConn(rs,stmt,conn); } return list; } public void executeUpdate(String sql){ Connection conn = null; Statement stmt = null; try { conn = startConn(conn); stmt = conn.createStatement(); stmt.executeUpdate(sql); } catch (SQLException e) { System.out.println("修改,插入或者刪除數(shù)據(jù)庫(kù)數(shù)據(jù)時(shí)發(fā)生錯(cuò)誤!"); }finally{ closeConn(stmt,conn); } } public void closeConn(ResultSet rs,Statement stmt,Connection conn){ try { if(rs != null){ rs.close(); } if(stmt != null){ stmt.close(); } if(conn != null){ conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block System.out.println("關(guān)閉數(shù)據(jù)庫(kù)的時(shí)候發(fā)生錯(cuò)誤!"); } } public void closeConn(Statement stmt,Connection conn){ try { if(stmt != null){ stmt.close(); } if(conn != null){ conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block System.out.println("關(guān)閉數(shù)據(jù)庫(kù)的時(shí)候發(fā)生錯(cuò)誤!"); } }}
需要拼接字符串, 因?yàn)閕d和student是字符串, 在SQL里需要加單引號(hào):
StringBuilder?builder?=?new?StringBuilder();
builder.append("insert?into?student?values");
builder.append("('");
builder.append(id);
builder.append("','");
builder.append(password);
builder.append("')");
String?st?=?builder.toString();
或者使用setString的方式:
Connection?conn?=?DriverManager.getConnection(url);
PreparedStatement?ps?=?conn.prepareStatement("insert?into?student?values?(?,??)");
pstmt.setString(1,?id);?????????????//?設(shè)置第1個(gè)參數(shù)的值為字符串
pstmt.setString(2,?password);????//?設(shè)置第2個(gè)參數(shù)的值為字符串
pstmt.execute();
當(dāng)前文章:java數(shù)據(jù)庫(kù)寫(xiě)入代碼 java創(chuàng)建數(shù)據(jù)庫(kù)代碼
分享地址:http://chinadenli.net/article12/dodshgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開(kāi)發(fā)、電子商務(wù)、網(wǎng)站營(yíng)銷(xiāo)、、軟件開(kāi)發(fā)、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)