81,092
社区成员
发帖
与我相关
我的任务
分享
package s2jsp.t76.dao;
import java.sql.*;
import java.util.List;
import org.apache.log4j.*;
public class BaseDao {
private final static String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";//数据库驱动
private final static String URL="jdbc:sqlserver://localhost:1433;databaseName=BaseDao";//连接路径
private final static String DBNAME="sa";//数据库登陆名
private final static String PWD="sa";//密码
private List<String> param;
private Logger log=Logger.getLogger(BaseDao.class);
private Connection con;
private PreparedStatement psmt;
private ResultSet rs;
/**
* 获取数据库连接对象的方法
*
*/
public Connection getCon() {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
log.debug(e);
}
try {
con=DriverManager.getConnection(URL,DBNAME,PWD);
} catch (SQLException e) {
// TODO Auto-generated catch block
log.debug(e);
}
}
//执行增删改的方法
public int doUpdate(String sql,List<String> str)
{
int x=0;
con=getCon();
try {
psmt=con.prepareStatement(sql);
if(str!=null)
{//设置处理对象的参数值
for(int i=0;i<str.size();i++){
psmt.setString(i+1, str.get(i));//设置参数
}
}
x=psmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
log.debug(e);
}
return x;
}
/**
* 关闭的方法,释放资源
*
*/
public void doClose()
{
try {
if(rs!=null)
{
rs.close();
}
if(psmt!=null)
{
psmt.close();
}
if(con!=null)
{
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
log.debug(e);
}
}
}
import Java.sql.*;
public class Test {
public static void main(String[] srg) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver "; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=sample "; //连接服务器和数据库sample
String userName = "sa "; //默认用户名
String userPwd = "123456 "; //密码
Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println( "Connection Successful! "); //如果连接成功 控制台输出Connection Successful!
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.sql.*;
public class Test {
public static void main(String[] srg) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=sample"; //连接服务器和数据库sample
String userName = "sa"; //默认用户名
String userPwd = "sa"; //密码
Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println( "Connection Successful! "); //如果连接成功 控制台输出Connection Successful!
} catch (Exception e) {
e.printStackTrace();
}
}
}
就是指明Class.forName(driverName);
这里错啊