我希望使用数据源方式连接SQL2005,希望高手指点整个解决方案,不胜感激。我的环境是win2003+sql2005+jdk1.5+tomcat5.5。
解决方案 »
- 使用 uploadify ,上传图片成功重命名后,如何在前台取得地址预览?
- IE打印
- 杂做论坛呢???
- 我想问下,学JSP的要学AJAX的哪些框架
- 使用PreparedStatement插入字符的最大值是如何限制的
- 关于httpclient中文乱码求助!!!
- iText 生成PDF 报错的问题
- 谁能帮帮我,关于jsp页面计算并写入数据表的问题,新手。。跪求高手了。。。
- 求jsp入门好源码和例程
- Error: oracle/jdbc/driver/oracledriver has been reported,这是怎么回事,谢谢!
- 一个在JSP页面或者Servlet中使用javascript的问题
- 请教:修改数据的问题?
// First we set up the BasicDataSource.
// Normally this would be handled auto-magically by
// an external configuration, but in this example we'll
// do it manually.
//
System.out.println("Setting up data source.");
DataSource dataSource = setupDataSource(args[0]);
System.out.println("Done."); //
// Now, we can use JDBC DataSource as we normally would.
//
Connection conn = null;
Statement stmt = null;
ResultSet rset = null; try {
System.out.println("Creating connection.");
conn = dataSource.getConnection();
System.out.println("Creating statement.");
stmt = conn.createStatement();
System.out.println("Executing statement.");
rset = stmt.executeQuery(args[1]);
System.out.println("Results:");
int numcols = rset.getMetaData().getColumnCount();
while(rset.next()) {
for(int i=1;i<=numcols;i++) {
System.out.print("\t" + rset.getString(i));
}
System.out.println("");
}
} catch(SQLException e) {
e.printStackTrace();
} finally {
try { rset.close(); } catch(Exception e) { }
try { stmt.close(); } catch(Exception e) { }
try { conn.close(); } catch(Exception e) { }
}
} public static DataSource setupDataSource(String connectURI) {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
ds.setUsername("scott");
ds.setPassword("tiger");
ds.setUrl(connectURI);
return ds;
} public static void printDataSourceStats(DataSource ds) throws SQLException {
BasicDataSource bds = (BasicDataSource) ds;
System.out.println("NumActive: " + bds.getNumActive());
System.out.println("NumIdle: " + bds.getNumIdle());
} public static void shutdownDataSource(DataSource ds) throws SQLException {
BasicDataSource bds = (BasicDataSource) ds;
bds.close();
}
}
我觉得你还是用JDBC好,你的环境都配好了没?那些系统补丁,SQL2005补丁,JDBC补丁都安装好,理论上和在2000上是一样的,只是JDBC补丁包的顺序不同了!得改下!
import java.sql.*;
public class ConnectDB {
Connection connection;
String jdbcDriverClassName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String jdbcURL = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName = ShoeManager";
String user = "share";
String password = "java";
public ConnectDB() {
try{
try {
Class.forName(jdbcDriverClassName);
//System.out.print("hello");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection = DriverManager.getConnection(jdbcURL,user,password);
}
catch(SQLException e){
e.printStackTrace();
}
// TODO Auto-generated constructor stub
}
public Connection getConnection(){
return connection;
}}
import java.sql.*;
import java.util.Vector;import javax.swing.JOptionPane;public class Operation {
Connection connection;
Statement statement;
ResultSet result ;
public Operation() {
try{
connection = (new ConnectDB()).getConnection();
statement = connection.createStatement();
}
catch(SQLException e){
e.printStackTrace();
}
// TODO Auto-generated constructor stub
}
public boolean productadd(String sql){
try{
statement.executeUpdate(sql);
statement.close();
connection.close();
return true;
}
catch(SQLException e){
e.printStackTrace();
return false;
}
//return true;
}
public ResultSet query(String sql){
try{
result = statement.executeQuery(sql);
if(result.next())
{
//JOptionPane.showMessageDialog(null,"已经定位到第一条记录了!");
return result;
}
else
{
JOptionPane.showMessageDialog(null,"不存在您输入的用户!");
return null;
}
//statement.close();
//connection.close();
}
catch(SQLException e){
e.printStackTrace();
return null;
}
//return true;
}}
2 在具体应用中的web.xml中配置对所配置数据源的引用
3 在应用中使用数据源由于是使用数据源的方式,所以隔离了针对具体数据库的具体操作的不同,因此,前端应用与底层数据库相对无关。