JDBC连接SQL server报错
弄了几天,也没找出原因。报错如下:
[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]无法打开登录 'spring-hibernate' 中请求的数据库。登录失败。
hibernate配置文件:
<session-factory>
<property name="connection.username"> sa </property>
<property name="connection.url">
jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=spring-hibernate
</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="myeclipse.connection.profile">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="connection.password"> sa </property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<mapping resource="com/raising/usermgr/model/User.hbm.xml" />
<mapping resource="com/raising/usermgr/model/Log.hbm.xml" />
</session-factory>
User配置文件:
<hibernate-mapping>
<class name="com.raising.usermgr.model.User" table="t_user">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="50" not-null="true" />
</property>
<property name="sex" type="java.lang.String">
<column name="sex" length="4" not-null="true" />
</property>
<property name="phone" type="java.lang.String">
<column name="phone" length="50" not-null="true" />
</property>
</class>
</hibernate-mapping>
Log配置文件:
<hibernate-mapping>
<class name="com.raising.usermgr.model.Log" table="t_log">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="type" type="java.lang.String">
<column name="type" length="50" not-null="true" />
</property>
<property name="desc" type="java.lang.String">
<column name="desc" length="50" not-null="true" />
</property>
<property name="time" type="java.util.Date">
<column name="time" length="50" not-null="true" />
</property>
</class>
</hibernate-mapping>
创建表的方法:
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
使用自定义连接数据库方法可以访问数据库。不知道问题出在哪?哪位大哥知道麻烦说下。
下面方法可以访问数据库:
public class DatabaseConn {
private Connection conn;
private static Statement stmt;
private String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=spring_hibernate";
private String classforname = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String uid = "sa";
private String pwd = "sa";
public DatabaseConn() {
}
public Connection getConnection() {
try {
Class.forName(classforname);
if (conn == null ¦ ¦ conn.isClosed())
conn = DriverManager.getConnection(url, uid, pwd);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
return conn;
}
public static void main(String[] args) throws SQLException {
DatabaseConn conn = new DatabaseConn();
Connection con = conn.getConnection();
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from test");
if(rs.next()){
while (rs.next()) {
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
}else{
System.out.println("无法获取数据");
}
}