When I was learning j2ee, I used jdbc to connect to ms SQL server 2000. At the beginning, everything was normal, but then I encountered a headache, after we read data from the table and put it in the ResultSet, we must read the column values in the column order.
For example, if the table contains three columns: id, name, address, and the result obtained by the conditional query is placed in the ResultSet. If the result is displayed, write the code as follows:
Java. SQL. ResultSet employees;
While (employees. next ()){
Employees. getString ("address ");
Employees. getString ("name ");
}
When running, the "column can not be re-read" error will occur. In this case, the two statements will be reversed (this is only applicable to connecting ms SQL Server 2000)
The database connection code is as follows:
Try {
Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver ");
Conn =
DriverManager. getConnection (
"Jdbc: microsoft: sqlserver: // localhost: 1433; DatabaseName = Hotel; User = sa; Password = sa ");
// Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
// Conn = DriverManager. getConnection ("jdbc: odbc: hotel ");
} Catch (SQLException e ){
System. err. println (e );
} Catch (ClassNotFoundException e ){
System. err. println (e );
}