该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
基本上按书打的吧 提示 the url cannot be null 自己实在是找不着错误啦
向各位求助 虽说不一定有人看吧。。。。。
import java.sql.*;
import java.io.*;
import java.util.*; class a
{
public static void main(String args[])
{
try
{
runTest();
}
catch(SQLException ex)
{
while(ex!=null)
{
ex.printStackTrace();
ex=ex.getNextException();
}
}
catch(IOException ex)
{
ex.printStackTrace();
}
} public static void runTest() throws SQLException,IOException
{
Connection conn=getConnection();
try
{
Statement stat=conn.createStatement();
stat.execute("CREATE TABLE Greetings(Message CHAR(20))");
stat.execute("INSERT INTO Greetings VALUES('hello world')");
ResultSet result=stat.executeQuery("SELETE * FROM Greetings");
result.next();
System.out.println(result.getString(1));
stat.execute("DROP TABLE Greetings");
}
finally
{
conn.close();
}
}
public static Connection getConnection() throws SQLException,IOException
{
Properties props=new Properties();
FileInputStream in=new FileInputStream("database.properties");
props.load(in);
in.close(); String drivers=props.getProperty("jdbc.drivers");
if(drivers!=null)
System.setProperty("jdbc.drivers",drivers);
String url=props.getProperty("jdbc.url");
String userName=props.getProperty("jdbc.username");
String password=props.getProperty("jdbc.password");
return DriverManager.getConnection(url,userName,password);
}
}
database property:
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://localhost:1433/a
datebaseName=a
userName=sa
password=fangyou