怎么连接数据库?

wangqing01 2004-09-08 12:42:32
是 SQL server
...全文
104 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
pdw2009 2004-09-08
  • 打赏
  • 举报
回复
连接数据库有4种方式,希望你到CSDN的文中心去看看大哥大姐们整理出的JDBC资料。。。
Frank1982 2004-09-08
  • 打赏
  • 举报
回复
/*
* DBOperation.java
*
* Created on 2004年7月19日, 上午9:52
*/

package Autosale;

import java.sql.*;

/**
*
* @author user
*/
public class DBOperation {

/* jdbc-odbc bridge driver */
private String strDbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
/* odbc data source */
private String strConn = "jdbc:odbc:autosale";
/* connection */
private Connection conn = null;
/* data set */
private ResultSet result = null;
/* PreparedStatement */
//private PreparedStatement pst = null;
/* database user */
private String strUser = "Frank";
/* user password */
private String strPassword = "";

/**
* constructor
*/
public DBOperation() {
try{
Class.forName( strDbDriver );
} catch( ClassNotFoundException ex ) {
System.err.println( ex.getMessage() );
}
}

/**
* constructor
* @param strUser database user
* @param strPassword user password
*/
public DBOperation(String strUser, String strPassword) {
this();
this.strUser = strUser;
this.strPassword = strPassword;
}


/**
* execute query and retrive the data
* @param strSql SQL string( select )
* @return result data
*
* @throws SQLException
*/
public ResultSet executeQuery( String strSql ) throws SQLException {
// clear result set
result = null;

// get Connection
conn = DriverManager.getConnection( strConn, strUser, strPassword );
Statement st = conn.createStatement();

// query
return st.executeQuery( strSql );
}

/**
* modify the data in database
* @param strSql SQL string ( delete, insert and update )
* @return execute result
* -1: error
* other: normal(the count of the data rows modified )
*
* @throws SQLException
*/
public int executeUpdate( String strSql ) throws SQLException {
// return value
int nRetValue = -1;

// get connection
conn = DriverManager.getConnection( strConn, strUser, strPassword );
Statement st = conn.createStatement();

// update
nRetValue = st.executeUpdate( strSql );

// close Statement and Connection
st.close();
conn.close();

return nRetValue;
}


}

这个是使用odbc的
你设置一下桥接就可以了
tyonggang 2004-09-08
  • 打赏
  • 举报
回复
网络上很多的。呵呵。
给你一个参考一下。
package Util;

import java.io.*;
import java.sql.*;
public class DBConn
{

public static Connection GetDBConn()
{
Connection conn=null;
try{
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/aa","root","");
System.out.println("Connect ok");
}
catch(Exception e)
{
e.printStackTrace();
}
return conn;
}
}
我用的时mysql的驱动,你去下一个,然后放到web-inf/lib目录下
AHUA1001 2004-09-08
  • 打赏
  • 举报
回复
老大,你问的太抽象了。不是想让别人帮你作吧。
一般这样的例子很多书上都有,在网络上也可以搜索到很多的。

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧