用這個類吧.好的話,給我加加分.

文登ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
import java.sql.*;
/**
* @功能: 一個JDBC的本地化API連接類,封裝了數(shù)據(jù)操作方法,只用傳一個SQL語句即可
* @作者: 李開歡
* @日期: 2007/
*/
public class ConnectionDemo {
/*
* 這里可以將常量全部放入另一個類中,以方便修改
*/
private static Connection conn;
private static Statement ps;
private static ResultSet rs;
private static final String DRIVER = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
private static final String USER ="sa";
private static final String PASS = "sa";
public ConnectionDemo() {
// TODO Auto-generated constructor stub
ConnectionDemo.getConnection();
}
public static Connection getConnection(){
System.out.println("連接中...");
try {
Class.forName(ConnectionDemo.DRIVER);
conn = DriverManager.getConnection(ConnectionDemo.URL, ConnectionDemo.USER, ConnectionDemo.PASS);
System.out.println("成功連接");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static Statement getStatement(String sql){
System.out.println("執(zhí)行SQL語句中...");
try {
ps = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
if(sql.substring(0, 6).equals("select")){
rs = ps.executeQuery(sql);
System.out.println("執(zhí)行完查詢操作,結(jié)果已返回ResultSet集合");
}else if(sql.substring(0, 6).equals("delete")){
ps.executeUpdate(sql);
System.out.println("已執(zhí)行完畢刪除操作");
}else if(sql.substring(0, 6).equals("insert")){
ps.executeUpdate(sql);
System.out.println("已執(zhí)行完畢增加操作");
}else{
ps.executeUpdate(sql);
System.out.println("已執(zhí)行完畢更新操作");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ps;
}
public static ResultSet getResultSet(){
System.out.println("查詢結(jié)果為:");
return rs;
}
public static void closeConnection(){
System.out.println("關(guān)閉連接中...");
try {
if (rs != null) {
rs.close();
System.out.println("已關(guān)閉ResultSet");
}
if (ps != null) {
ps.close();
System.out.println("已關(guān)閉Statement");
}
if (conn != null) {
conn.close();
System.out.println("已關(guān)閉Connection");
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ConnectionDemo.getConnection();
String sql = "delete from type where id = 1";
ConnectionDemo.getStatement(sql);
String sql2 = "insert into type values(1,'教學(xué)設(shè)備')";
ConnectionDemo.getStatement(sql2);
String sql1 = "select * from type";
ConnectionDemo.getStatement(sql1);
ResultSet rs = ConnectionDemo.getResultSet();
System.out.println("編號 "+"類 型");
try {
while(rs.next()){
System.out.print(" "+rs.getInt(1)+" ");
System.out.println(rs.getString(2));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ConnectionDemo.closeConnection();
}
}
//此類為連接數(shù)據(jù)庫并進行數(shù)據(jù)庫的操作
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Conn {
private static Connection conn = null;
private static Statement st = null;
private static ResultSet rs = null;
//建立數(shù)據(jù)庫的連接
以上的代碼都不如哥的 且看哥是怎么寫條理清晰的代碼的!!!
package dbconnection //java 中不存在沒有包的類(講解詳細(xì)因為項目需要)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;//引入sql數(shù)據(jù)庫包
public class DBConnection{
private Connection conn=null;
private Statement stmt=null;
private Result rs=null;
private String jdbc="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String driverManager="jdbc:sqlserver://localhost:1433;databasename=HcitPos";
private String user="admin";
private int password="admin";
public DBConnection{
try{
Class.forName("jdbc");
conn.getConnection("driverManager");
}
catch(Exception e){}
}
public selectMethod(String sql){
stmt=conn.createStatement();
rs=stmt.extcuteQuery("sql");
while(rs.next()){
String title=rs.getString("title");//利用javaBean獲得數(shù)據(jù)庫中的屬性
String name=rs.getString("name");
.......
System.out.println("title");
System.out.println("name");
......
//當(dāng)然數(shù)據(jù)庫的操作有很多 這里簡單介紹下功能的實現(xiàn)
}
}
public void closeDB(){
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}
}
1 將數(shù)據(jù)庫的JDBC驅(qū)動加載到classpath中,在基于JAVAEE的WEB應(yīng)用實際開發(fā)過程中,通常要把目標(biāo)數(shù)據(jù)庫產(chǎn)品的JDBC驅(qū)動復(fù)制到WEB-INF/lib下.
2 加載JDBC驅(qū)動,并將其注冊到DriverManager中,下面是一些主流數(shù)據(jù)庫的JDBC驅(qū)動加裁注冊的代碼:
//Oracle8/8i/9iO數(shù)據(jù)庫(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
//Sql Server7.0/2000數(shù)據(jù)庫
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
//DB2數(shù)據(jù)庫
Class.froName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();
//Informix數(shù)據(jù)庫
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
//Sybase數(shù)據(jù)庫
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
//MySQL數(shù)據(jù)庫
Class.forName("com.mysql.jdbc.Driver").newInstance();
//PostgreSQL數(shù)據(jù)庫
Class.forNaem("org.postgresql.Driver").newInstance();
3 建立數(shù)據(jù)庫連接,取得Connection對象.例如:
//Oracle8/8i/9i數(shù)據(jù)庫(thin模式)
String url="jdbc:oracle:thin:@localhost:1521:orcl";
String user="scott";
String password="tiger";
Connection conn=DriverManager.getConnection(url,user,password);
--完整的太多了!我已經(jīng)把完整的代碼發(fā)到你QQ郵箱了!
網(wǎng)頁題目:java控制數(shù)據(jù)庫代碼 java控制數(shù)據(jù)庫代碼大全
地址分享:http://chinadenli.net/article46/hghjeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、面包屑導(dǎo)航、移動網(wǎng)站建設(shè)、服務(wù)器托管、搜索引擎優(yōu)化、品牌網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)