代码如下:
package src;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;/**
 * @author  、√又乱了
 *
 */public class BaseDao {
private static final String DRIVER_CLASS="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bbs";
private static final String DBUSER="sa";
private static final String DBPASS="li121201391!";

/**
 * 建立连接
 * @return
 * @throws ClassNotFoundException
 * @throws SQLException
 */ public Connection getConn() throws ClassNotFoundException, SQLException
{
//加载驱动
Class.forName(DRIVER_CLASS);
Connection conn=DriverManager.getConnection(URL);
return conn;
}
/**
 * 执行添删改操作,返回受影响的行数
 * @return
 * @throws ClassNotFoundException
 * @throws SQLException
 */
public int executeSQL(String Mysql,String[] param)
{
Connection conn=null;
int num=0;
PreparedStatement pStatement=null;
try {
conn=getConn();
pStatement=conn.prepareStatement(Mysql);
if(param!=null)
{
for(int i=0;i<param.length;i++)
{
pStatement.setString(i+1, param[i]);
}
}
num=pStatement.executeUpdate();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return num;
}
/**
 * @author  、√又乱了
 *释放资源
 */
public void closeAll(Connection conn,PreparedStatement pStatement,ResultSet rs)
{
if(rs!=null)
{
//关闭结果集
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pStatement!=null)
{
//关闭preparedStatement
try {
pStatement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
if(conn!=null||conn.isClosed()==false)
{
//关闭连接
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args)
{
BaseDao bsDao=new BaseDao();
String sql="insert into dbo.TBL_USER(uName,uPass,head,regTime,gender) values(?,?,?,?,?)";
String[] user=new String[5];
user[0]="李四";
user[1]="12345";
user[2]="卡通";
user[3]="2009-02-02";
user[4]="2";
int count=bsDao.executeSQL(sql, user);
System.out.println("成功插入了"+count+"行数据。");
}
}
用JDBC连接数据库提示的错误:
java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=bbs
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:207)
at src.BaseDao.getConn(BaseDao.java:31)
at src.BaseDao.executeSQL(BaseDao.java:46)
at src.BaseDao.main(BaseDao.java:117)
这是什么原因?有在网上百度了一下,有人说是没装在sqljdbc驱动包的问题。但是我已经装了,不知道为什么。。在线等待