您的位置:首页 > 数据库

JDBC连接数据库例子

2011-03-08 15:20 381 查看
// Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov  Date: 2007-12-25 8:17:30
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3)
// Source File Name:   DbConnection.java
package emanage.db;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbConnection {
public DbConnection() {
conn = null;
stmt = null;
rset = null;
}
public boolean openConnection() {
/**
* 2000的数据库驱动和连接字符串.
*/
//		 String jdbc = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
//		 String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ls";
/**
* 2005的数据库驱动和连接字符串
*/
String jdbc = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String url = "jdbc:sqlserver://192.168.18.186:1433;databaseName=ls";
String user = "sa";
String password = "dujian";
try {
Class.forName(jdbc);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out
.println("jdbc /u767B/u9646/u8FC7/u7A0B/u4E2D/u51FA/u73B0/u9519/u8BEF"
+ e.getMessage());
return false;
}
try {
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
System.out
.println("/u751F/u6210Connection/u8FC7/u7A0B/u4E2D/u51FA/u73B0/u9519/u8BEF/uFF1A"
+ e.getMessage());
return false;
}
return true;
}
public ResultSet executeImg(String sql) throws SQLException {
System.out.print(conn);
stmt = conn.createStatement();
return stmt.executeQuery(sql);
}
public ResultSet executeQuery(String query) throws SQLException {
stmt = conn.createStatement(1004, 1007);
rset = stmt.executeQuery(query);
return rset;
}
public int executeDelete(String query) throws SQLException {
stmt = conn.createStatement(1004, 1007);
j = stmt.executeUpdate(query);
return j;
}
public int executeUpdate(String query) throws SQLException {
stmt = conn.createStatement(1004, 1007);
count = stmt.executeUpdate(query);
// System.out.println(count);
if (stmt != null)
stmt.close();
return count;
}
public boolean execute1(String query) throws SQLException {
stmt = conn.createStatement(1004, 1007);
boolean bl = stmt.execute(query);
return bl;
}

public int insertImgToDb(String sql, String img) throws SQLException,
IOException {
PreparedStatement ps = conn.prepareStatement(sql);
File file = new File("d://Tomcat 6.0//webapps//"
+ PropertiesUtils.getProperty("bbAddr") + "//jre//" + img);
FileInputStream input = new FileInputStream(file);
ps.setBinaryStream(1, input, input.available());
int num = ps.executeUpdate();
if (ps != null) {
ps.close();
}
if (input != null) {
input.close();
}
file.delete();
return num;
}
public int insertImgToDb(String sql, String img, String img1)
throws SQLException, IOException {
PreparedStatement ps = conn.prepareStatement(sql);
File file = new File("d://Tomcat 6.0//webapps//"
+ PropertiesUtils.getProperty("bbAddr") + "//jre//" + img);
File file_0 = new File("d://Tomcat 6.0//webapps//"
+ PropertiesUtils.getProperty("bbAddr") + "//jre//" + img1);
FileInputStream input = new FileInputStream(file);
FileInputStream input_0 = new FileInputStream(file_0);
ps.setBinaryStream(1, input, input.available());
ps.setBinaryStream(2, input_0, input_0.available());
int num = ps.executeUpdate();
if (ps != null) {
ps.close();
}
if (input != null) {
input.close();
}
if (input_0 != null) {
input_0.close();
}
file.delete();
file_0.delete();
return num;
}
public void close() throws SQLException {
if (rset != null)
rset.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
}
protected void finalize() throws Throwable {
close();
}
Connection conn;
Statement stmt;
ResultSet rset;
public int j;
private int count;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息