通过端口 1433 连接到主机 127.0.0.1 的 TCP/IP 连接失败。错误:“Connection refused: connect。
JSP连接SQL2005数据库出现错误,SQL默认TCP/IP端口号是1433啊,怎么回事?代码如下:packagebeans;importjava.sql.Connec...
JSP连接SQL2005数据库出现错误,SQL默认TCP/IP端口号是1433啊,怎么回事?
代码如下:
package beans;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
public class DBUtil {
public Connection getConnection() throws ClassNotFoundException, SQLException
{
Connection conn=null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=newsdb;user=sa;password=sa");
return conn;
}
public ResultSet executeSelect(String sql)
{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
conn=getConnection();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stmt=conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
rs=stmt.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public int executeUpdate(String sql) throws SQLException, ClassNotFoundException
{
Connection conn=null;
Statement stmt=null;
int iCount=0;
conn=getConnection();
stmt=conn.createStatement();
iCount=stmt.executeUpdate(sql);
return iCount;
}
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DBUtil db=new DBUtil();
ResultSet rs=null;
rs=db.executeSelect("Select f_id,f_title,f_date from t_news");
try {
while (rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} 展开
代码如下:
package beans;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
public class DBUtil {
public Connection getConnection() throws ClassNotFoundException, SQLException
{
Connection conn=null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=newsdb;user=sa;password=sa");
return conn;
}
public ResultSet executeSelect(String sql)
{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
conn=getConnection();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stmt=conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
rs=stmt.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
public int executeUpdate(String sql) throws SQLException, ClassNotFoundException
{
Connection conn=null;
Statement stmt=null;
int iCount=0;
conn=getConnection();
stmt=conn.createStatement();
iCount=stmt.executeUpdate(sql);
return iCount;
}
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DBUtil db=new DBUtil();
ResultSet rs=null;
rs=db.executeSelect("Select f_id,f_title,f_date from t_news");
try {
while (rs.next())
{
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));
}
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} 展开
展开全部
public class DBUtil {
public Connection getConnection() throws ClassNotFoundException, SQLException
{
Connection conn=null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=newsdb;user=sa;password=sa");
return conn;
连接被拒,一般是验证身份出了问题。
上面这个语句定义了前台到Database的验证,但是你的数据库允许SA弱口令登录否???
public Connection getConnection() throws ClassNotFoundException, SQLException
{
Connection conn=null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=newsdb;user=sa;password=sa");
return conn;
连接被拒,一般是验证身份出了问题。
上面这个语句定义了前台到Database的验证,但是你的数据库允许SA弱口令登录否???
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询