用這個類吧.好的話,給我加加分.
創新互聯是專業的哈巴河網站建設公司,哈巴河接單;提供網站建設、成都網站建設,網頁設計,網站設計,建網站,PHP網站建設等專業做網站服務;采用PHP框架,可快速的進行哈巴河網站開發網頁制作和功能擴展;專業做搜索引擎喜愛的網站,專業的做網站團隊,希望更多企業前來合作!
import java.sql.*;
/**
* @功能: 一個JDBC的本地化API連接類,封裝了數據操作方法,只用傳一個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("執行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("執行完查詢操作,結果已返回ResultSet集合");
}else if(sql.substring(0, 6).equals("delete")){
ps.executeUpdate(sql);
System.out.println("已執行完畢刪除操作");
}else if(sql.substring(0, 6).equals("insert")){
ps.executeUpdate(sql);
System.out.println("已執行完畢增加操作");
}else{
ps.executeUpdate(sql);
System.out.println("已執行完畢更新操作");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ps;
}
public static ResultSet getResultSet(){
System.out.println("查詢結果為:");
return rs;
}
public static void closeConnection(){
System.out.println("關閉連接中...");
try {
if (rs != null) {
rs.close();
System.out.println("已關閉ResultSet");
}
if (ps != null) {
ps.close();
System.out.println("已關閉Statement");
}
if (conn != null) {
conn.close();
System.out.println("已關閉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,'教學設備')";
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();
}
}
import java.sql.Connection。
import java.sql.DriverManager; ?
import java.sql.PreparedStatement; ?
import java.sql.ResultSet; ?
import java.sql.SQLException;
import javax.naming.Context; ?
import javax.naming.InitialContext; ?
import javax.naming.NamingException; ?
import javax.sql.DataSource;
public class DBCon {
//數據庫驅動對象
public static final String DRIVER="oracle.jdbc.driver.OracleDriver";
//數據庫連接地址(數據庫名)
public static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
//登陸名
public static final String USER="FM";
//登陸密碼
public static final String PWD="FM";
//創建數據庫連接對象
private Connection con=null;
//創建數據庫預編譯對象
private PreparedStatement ps=null;
//創建結果集
private ResultSet rs=null;
//創建數據源對象
public static DataSource source=null;
// ?//靜態代碼塊 ?
// ?static{ ?
// ?
// ? ? ?//初始化配置文件context ?
// ? ? ?try { ?
// ? ? ? ? ?Context context=new InitialContext(); ?
// ? ? ? ? ?source=(DataSource)context.lookup("java:comp/env/jdbc/webmessage"); ?
// ? ? ?} catch (Exception e) { ?
// ? ? ? ? ?// TODO Auto-generated catch block ?
// ? ? ? ? ?e.printStackTrace(); ?
// ? ? ?} ?
// ?
// ?
// ?}
/**
* 獲取數據庫連接
*/
public Connection getCon(){
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con=DriverManager.getConnection(URL,USER,PWD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
} ?
// ?/** ?
// ? * 獲取數據庫連接 ?
// ? */ ?
// ?public Connection getCon(){ ?
// ?
// ? ? ?try { ?
// ? ? ? ? ?con=source.getConnection(); ?
// ? ? ?} catch (SQLException e) { ?
// ? ? ? ? ?// TODO Auto-generated catch block ?
// ? ? ? ? ?e.printStackTrace(); ?
// ? ? ?} ?
// ?
// ? ? ?return con; ?
// ?} ?
/**
* 關閉所有資源
*/
public void closeAll(){
if(rs!=null)
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(ps!=null)
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(con!=null)
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} ?
}
/**
* @param sql數據庫更新(增、刪、改) 語句
* @param pras參數列表(可傳,可不傳,不傳為NULL,以數組形式存在)
* @return 返回受影響都行數
*/
public int update(String sql,String... pras){
int resu=0;
con=getCon();
try {
ps=con.prepareStatement(sql);
for(int i=0;ipras.length;i++){
ps.setString(i+1,pras[i]);
}
resu=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
closeAll();
}
return resu;
}
/**
* @param sql數據庫查詢語句
* @param pras參數列表(可傳,可不傳,不傳為NULL,以數組形式存在)
* @return 返回結果集
*/
public ResultSet query(String sql,String... pras){
con=getCon();
try {
ps=con.prepareStatement(sql);
if(pras!=null)
for(int i=0;ipras.length;i++){
ps.setString(i+1, pras[i]);
}
rs=ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
} ?
}
mport java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class ConnDB
{
public static void main(String[] args)
{
try
{
//我這里用mysql數據庫
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mytest";
Connection conn = DriverManager.getConnection(url, "root", "123");
String sql = "select * from user limit ?,?";//這里沒有括號
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, 1);//為問號賦值
ps.setInt(2, 3);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
System.out.println(rs.getString(2));
}
rs.close();
ps.close();
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
希望對你有幫助
網站名稱:javasql代碼 java代碼庫
標題URL:http://vcdvsql.cn/article10/hhpsdo.html
成都網站建設公司_創新互聯,為您提供標簽優化、網站制作、外貿建站、域名注冊、品牌網站建設、企業網站制作
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯