Java操作DB
自學了
代碼很粗糙
另外
下面是我做的一個Demo
一
* 簡單的數據連接工具
* File: DBUtils
* User: leizhimin
* Date:
*/
public class DBUtils {
public static final String url =
public static final String username =
public static final String password =
public static final String driverClassName =
/**
* 獲取數據庫連接Connection
*
* @return 數據庫連接Connection
*/
public static Connection makeConnection() {
Connection conn = null;
try {
Class
} catch (ClassNotFoundException e) {
e
}
try {
conn = DriverManager
} catch (SQLException e) {
e
}
return conn;
}
public static void main(String args[]) {
testConnection();
}
/**
* 測試連接方法
*/
public static void testConnection() {
Connection conn = makeConnection();
try {
Statement stmt = conn
ResultSet rs = stmt
while (rs
String s
String s
System
}
rs
stmt
} catch (SQLException e) {
e
} finally {
try {
conn
} catch (SQLException e) {
e
}
}
}
}
二
import mon
import java
import java
import java
import java
/**
* DB
* File: TestXMLDAO
* User: leizhimin
* Date:
*/
public class TestDB
/**
* 預刪除表Customer
*
* @throws SQLException
*/
public static void testDropXMLTable() throws SQLException {
String drop_sql =
Connection conn = DBUtils
Statement stmt = conn
stmt
stmt
conn
}
/**
* 創建表
*
* @throws SQLException
*/
public static void testCreateXMLTable() throws SQLException {
String ct_sql =
Connection conn = DBUtils
Statement stmt = conn
stmt
stmt
conn
}
/**
* 插入數據
*
* @throws SQLException
* @throws IOException
*/
public static void testInsertXMLTable() throws SQLException
String xml =
String ins_sql =
Connection conn = DBUtils
conn
PreparedStatement pstmt = conn
byte[] b = xml
InputStream ins = new ByteArrayInputStream(b);
pstmt
pstmt
mit();
ins
pstmt
conn
}
/**
* XQuery查詢數據
*
* @throws SQLException
*/
public static void testQueryXMLTable() throws SQLException {
String query_sql =
Connection conn = DBUtils
Statement stmt = conn
ResultSet rs = stmt
StringBuffer xmls = new StringBuffer();
while (rs
xmls
}
System
stmt
conn
}
/**
* XQuery更新數據
*
* @throws SQLException
* @throws IOException
*/
public static void testUpdateXMLTable() throws SQLException
String xml =
String up_sql =
Connection conn = DBUtils
conn
PreparedStatement pstmt = conn
byte[] b = xml
InputStream ins = new ByteArrayInputStream(b);
pstmt
pstmt
mit();
ins
pstmt
conn
}
* 查詢xml列數據
*
* @throws SQLException
*/
public static void testQueryXMLColumn() throws SQLException {
String query_sql =
Connection conn = DBUtils
Statement stmt = conn
ResultSet rs = stmt
StringBuffer xmls = new StringBuffer();
while (rs
xmls
}
System
stmt
conn
}
/**
* 測試入口
*
* @param rags
* @throws Exception
*/
public static void main(String rags[]) throws Exception {
testDropXMLTable();
testCreateXMLTable();
testInsertXMLTable();
testQueryXMLTable();
testUpdateXMLTable();
testQueryXMLColumn();
}
}
三
<out xmlns=
<customerinfo xmlns=
Process finished with exit code
呵呵
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25786.html