如题,代码如下:
数据库连接类DBcon.java
- Java code
package db;import java.sql.Connection;import java.sql.DriverManager;public class DBcon { private String url; // 存储SQLSever连接路径 private String serverName; // 存储机器的名称 private String portNumber; // 存储端口名称 private String databaseName; // 存储数据库名称 private String userName; // 存储用户名称 private String password; // 存储密码 /* 设置连接数据库相关参数 */ public DBcon(){ url = "jdbc:jtds:sqlserver://"; serverName = "localhost"; portNumber = "1433"; databaseName = "JSP_Test"; userName = "sa"; password = ""; } /* 获取连接数据库路径并返回 */ private String getConnectionUrl() { return url + serverName + ":" + portNumber + ";databaseName=" + databaseName + ";"; } /* 获取Conncetion对象并返回 */ public Connection getConnection() { Connection con=null; try { Class.forName("net.sourceforge.jtds.jdbc.Driver"); // 加载Jdbc驱动程序 con = DriverManager.getConnection(getConnectionUrl(),userName,password); } catch (Exception e) { e.printStackTrace(); System.out.println("getConnection()内部跟踪错误:"+ e.getMessage()); } return con; }}
数据操作类
StudentImpl.java
- Java code
package impl;import pojo.Student;import db.DBcon;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;public class StudentImpl { public void saveStudent(Student student) { DBcon conn = new DBcon(); Connection con = null; PreparedStatement pre = null; con = conn.getConnection(); try { pre = con.prepareStatement("insert into t31_Student(sname,slikes) values('?','?')"); pre.setString(1, student.getSname()); pre.setString(2, student.getSadress()); pre.executeUpdate(); con.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("StudentImpl"); System.out.println(student.getSname()); System.out.println(student.getSadress()); } }
代码没有问题,但是一运行就提示:
Network error IOException: Connection refused: connect
向各位求解一下,谢谢各位了
------解决方案--------------------------------------------------------
明显的数据库 访问不到,用客户端看看 SQL SERVER 2008能访问不
------解决方案--------------------------------------------------------
2楼说的没错,通配符加什么引号。不过你这个错误似乎是因为连接数据库错误,在检查你的url、driver可以用myeclipse自带的myeclipse hibernate视图(右上方)试一下
------解决方案--------------------------------------------------------
明显是数据库拒绝连接