eclipse项目原本的数据库连接为sql server,我想把它改成mysql,但是修改完文件后,仍然报数据库连接错误 10
驱动jar包也加进去了。下面是修改后的JDBConnection文件的代码,及报错细节importjava.sql.Connection;importjava.sql.D...
驱动jar包也加进去了。下面是修改后的JDBConnection文件的代码,及报错细节
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBConnection
{
private final String url = "jdbc:mysql://localhost:3306/rm";
private final String userName = "root";
private final String password = "";
private Connection con = null;
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception ex) {
System.out.println("数据库加载失败");
}
}
public JDBConnection() {
}
public boolean creatConnection() {
try {
con = DriverManager.getConnection(url,userName,password);
//con.setAutoCommit(true);
}
catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("creatConnectionError!");
}
return true;
}
public boolean executeUpdate(String sql)
{
if (con == null) {
creatConnection();
}
try {
Statement stmt = con.createStatement();
int iCount = stmt.executeUpdate(sql);
System.out.println("操作成功,所影响的记录数为" + String.valueOf(iCount));
return true;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return false;
}
public java.sql.ResultSet executeQuery(String sql)
{
java.sql.ResultSet rs;
try
{
if (con == null) {
creatConnection();
}
Statement stmt = con.createStatement();
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
return null;
}
}
catch (SQLException e)
{
System.out.println(e.getMessage());
System.out.println("executeQueryError!");
return null;
}
return rs;
}
}
通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。请验证连接属性。确保 SQL Server 的实例正在主机上运行,且在此端口接受 TCP/IP 连接,还要确保防火墙没有阻止到此端口的 TCP 连接。”。
creatConnectionError! 展开
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBConnection
{
private final String url = "jdbc:mysql://localhost:3306/rm";
private final String userName = "root";
private final String password = "";
private Connection con = null;
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception ex) {
System.out.println("数据库加载失败");
}
}
public JDBConnection() {
}
public boolean creatConnection() {
try {
con = DriverManager.getConnection(url,userName,password);
//con.setAutoCommit(true);
}
catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("creatConnectionError!");
}
return true;
}
public boolean executeUpdate(String sql)
{
if (con == null) {
creatConnection();
}
try {
Statement stmt = con.createStatement();
int iCount = stmt.executeUpdate(sql);
System.out.println("操作成功,所影响的记录数为" + String.valueOf(iCount));
return true;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return false;
}
public java.sql.ResultSet executeQuery(String sql)
{
java.sql.ResultSet rs;
try
{
if (con == null) {
creatConnection();
}
Statement stmt = con.createStatement();
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
return null;
}
}
catch (SQLException e)
{
System.out.println(e.getMessage());
System.out.println("executeQueryError!");
return null;
}
return rs;
}
}
通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。请验证连接属性。确保 SQL Server 的实例正在主机上运行,且在此端口接受 TCP/IP 连接,还要确保防火墙没有阻止到此端口的 TCP 连接。”。
creatConnectionError! 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询