当前位置: 技术问答>java相关
jdbc连接数据库SQLSERVER2000时,JNDI的问题?急,高手解答,万分感谢!
来源: 互联网 发布时间:2015-09-26
本文导语: 程序如下: package myprojects.db; import java.sql.*; import javax.sql.*; import javax.naming.*; import java.awt.*; import java.awt.event.*; public class Sql2k extends Frame{ public Sql2k(){ addWindowListener ( new WindowAdapter() { public void windowCloin...
程序如下:
package myprojects.db;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import java.awt.*;
import java.awt.event.*;
public class Sql2k extends Frame{
public Sql2k(){
addWindowListener ( new WindowAdapter() {
public void windowCloing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public static void main(String args[]){
String sDBDriver ="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String sSql="select * from employees";
/** String sConnStr ="jdbc:microsoft:sqlserver://localhost:1433";
*/ String sLogin = "sa";
String sPassword="sa";
Connection conn=null;
ResultSet rs=null;
Statement stmt;
try{
Class.forName(sDBDriver);
}
catch(ClassNotFoundException e){
System.out.println(e);
}
try{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/Northwind");
conn = ds.getConnection(sLogin,sPassword);
stmt = conn.createStatement();
rs = stmt.executeQuery(sSql);
if(rs.next()){
String LastName = rs.getString("LastName");
rs.next();
System.out.println(LastName);
}
else{
System.out.println("There Were 0 employee records in the database");
}
rs.close();
stmt.close();
conn.close();
}
catch(SQLException e){
System.out.println(e);
}
catch(NamingException e){
System.out.println(e);
}
finally{
/** rs.close();
stmt.close();
conn.close();
*/ }
}
}
编译成功,但运行时出现如下错误:
javax.naming.NoInitialContextException: Need to specify class name in environmen
t or system property, or as an applet parameter, or in an application resource f
ile: java.naming.factory.initial
当把rs.close();stmt.close();conn.close();放到finally{}模块中出现如下错误:
--------------------Configuration: db - j2se --------------------
f:Program FilesXinox SoftwareJCreator ProMyProjectsdbSql2k.java:59: unreported exception java.sql.SQLException; must be caught or declared to be thrown
rs.close();
^
f:Program FilesXinox SoftwareJCreator ProMyProjectsdbSql2k.java:60: variable stmt might not have been initialized
stmt.close();
^
f:Program FilesXinox SoftwareJCreator ProMyProjectsdbSql2k.java:60: unreported exception java.sql.SQLException; must be caught or declared to be thrown
stmt.close();
^
f:Program FilesXinox SoftwareJCreator ProMyProjectsdbSql2k.java:61: unreported exception java.sql.SQLException; must be caught or declared to be thrown
conn.close();
^
4 errors
Process completed.
package myprojects.db;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import java.awt.*;
import java.awt.event.*;
public class Sql2k extends Frame{
public Sql2k(){
addWindowListener ( new WindowAdapter() {
public void windowCloing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
public static void main(String args[]){
String sDBDriver ="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String sSql="select * from employees";
/** String sConnStr ="jdbc:microsoft:sqlserver://localhost:1433";
*/ String sLogin = "sa";
String sPassword="sa";
Connection conn=null;
ResultSet rs=null;
Statement stmt;
try{
Class.forName(sDBDriver);
}
catch(ClassNotFoundException e){
System.out.println(e);
}
try{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/Northwind");
conn = ds.getConnection(sLogin,sPassword);
stmt = conn.createStatement();
rs = stmt.executeQuery(sSql);
if(rs.next()){
String LastName = rs.getString("LastName");
rs.next();
System.out.println(LastName);
}
else{
System.out.println("There Were 0 employee records in the database");
}
rs.close();
stmt.close();
conn.close();
}
catch(SQLException e){
System.out.println(e);
}
catch(NamingException e){
System.out.println(e);
}
finally{
/** rs.close();
stmt.close();
conn.close();
*/ }
}
}
编译成功,但运行时出现如下错误:
javax.naming.NoInitialContextException: Need to specify class name in environmen
t or system property, or as an applet parameter, or in an application resource f
ile: java.naming.factory.initial
当把rs.close();stmt.close();conn.close();放到finally{}模块中出现如下错误:
--------------------Configuration: db - j2se --------------------
f:Program FilesXinox SoftwareJCreator ProMyProjectsdbSql2k.java:59: unreported exception java.sql.SQLException; must be caught or declared to be thrown
rs.close();
^
f:Program FilesXinox SoftwareJCreator ProMyProjectsdbSql2k.java:60: variable stmt might not have been initialized
stmt.close();
^
f:Program FilesXinox SoftwareJCreator ProMyProjectsdbSql2k.java:60: unreported exception java.sql.SQLException; must be caught or declared to be thrown
stmt.close();
^
f:Program FilesXinox SoftwareJCreator ProMyProjectsdbSql2k.java:61: unreported exception java.sql.SQLException; must be caught or declared to be thrown
conn.close();
^
4 errors
Process completed.
|
send to me ru25@163.net