/**
@version 1.30 2004-08-05
@author Cay Horstmann
*/import java.io.*;
import java.util.*;
import java.sql.*;
/**
Executes all SQL statements in a file.
Call this program as
java -classpath driverPath:. ExecSQL commandFile
*/
public class ExecSQL
{
public static void main(String args[])
{
try
{
Scanner in;
if (args.length == 0)
in = new Scanner(System.in);
else
in = new Scanner(new File(args[0]));
Connection conn = getConnection();
try
{
Statement stat = conn.createStatement();
while (true)
{
if (args.length == 0) System.out.println("Enter command or EXIT to exit:");
if (!in.hasNextLine()) return; String line = in.nextLine();
if (line.equalsIgnoreCase("EXIT")) return;
try
{
boolean hasResultSet = stat.execute(line);
if (hasResultSet)
showResultSet(stat);
}
catch (SQLException e)
{
while (e != null)
{
e.printStackTrace();
e = e.getNextException();
}
}
}
}
finally
{
conn.close();
}
}
catch (SQLException e)
{
while (e != null)
{
e.printStackTrace();
e = e.getNextException();
}
}
catch (IOException e)
{
e.printStackTrace();
}
} /**
Gets a connection from the properties specified
in the file database.properties
@return the database connection
*/
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);
} /**
Prints a result set.
@param stat the statement whose result set should be
printed
*/
public static void showResultSet(Statement stat)
throws SQLException
{
ResultSet result = stat.getResultSet();
ResultSetMetaData metaData = result.getMetaData();
int columnCount = metaData.getColumnCount(); for (int i = 1; i <= columnCount; i++)
{
if (i > 1) System.out.print(", ");
System.out.print(metaData.getColumnLabel(i));
}
System.out.println(); while (result.next())
{
for (int i = 1; i <= columnCount; i++)
{
if (i > 1) System.out.print(", ");
System.out.print(result.getString(i));
}
System.out.println();
}
result.close();
}
}
@version 1.30 2004-08-05
@author Cay Horstmann
*/import java.io.*;
import java.util.*;
import java.sql.*;
/**
Executes all SQL statements in a file.
Call this program as
java -classpath driverPath:. ExecSQL commandFile
*/
public class ExecSQL
{
public static void main(String args[])
{
try
{
Scanner in;
if (args.length == 0)
in = new Scanner(System.in);
else
in = new Scanner(new File(args[0]));
Connection conn = getConnection();
try
{
Statement stat = conn.createStatement();
while (true)
{
if (args.length == 0) System.out.println("Enter command or EXIT to exit:");
if (!in.hasNextLine()) return; String line = in.nextLine();
if (line.equalsIgnoreCase("EXIT")) return;
try
{
boolean hasResultSet = stat.execute(line);
if (hasResultSet)
showResultSet(stat);
}
catch (SQLException e)
{
while (e != null)
{
e.printStackTrace();
e = e.getNextException();
}
}
}
}
finally
{
conn.close();
}
}
catch (SQLException e)
{
while (e != null)
{
e.printStackTrace();
e = e.getNextException();
}
}
catch (IOException e)
{
e.printStackTrace();
}
} /**
Gets a connection from the properties specified
in the file database.properties
@return the database connection
*/
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);
} /**
Prints a result set.
@param stat the statement whose result set should be
printed
*/
public static void showResultSet(Statement stat)
throws SQLException
{
ResultSet result = stat.getResultSet();
ResultSetMetaData metaData = result.getMetaData();
int columnCount = metaData.getColumnCount(); for (int i = 1; i <= columnCount; i++)
{
if (i > 1) System.out.print(", ");
System.out.print(metaData.getColumnLabel(i));
}
System.out.println(); while (result.next())
{
for (int i = 1; i <= columnCount; i++)
{
if (i > 1) System.out.print(", ");
System.out.print(result.getString(i));
}
System.out.println();
}
result.close();
}
}
下面是database.properties文件内容:
jdbc.drivers=com.microsoft.jdbc.sqlserver.SQLServerDriverjdbc.url=jdbc:microsoft:sqlserver://10.0.0.140:1433jdbc.username=zqyjdbc.password=820827运行时,出现如下问题:
1,不能连接数据库,但改用另一种方法连接则可以成功!
2,输入文件名books.sql,(此文件是一些SQL语句)
出现找不到books存储过程!
你是高手吗?
你能解决此问题吗/
另外,如果想问题得到解决,尽量给详细信息(特别是错误信息)
Class.forName(drivers).newInstance();
Class.forName(drivers).newInstance();
我用的是另一种连接方式,你再仔细看看,我用一个属性文件database.properties保存连接属性.再连接数据库.
这可是core java2 II的经典代码呀!
throws Exception
{
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");
Class.forName(drivers).newInstance(); //add
return DriverManager.getConnection(url , username, password);
}
jdbc.url=jdbc:microsoft:sqlserver://10.0.0.140:1433;databasename=yourdb再经典,不指定数据库名能连上? 你试试吧
数据库已在sql server2000中指定了默认数据库.你这种方法,我试过,不行,你还是没看懂用法呀.
至于你说不行,总有原因吧
等价于
Class.forName(drivers).newInstance(); 数据库已在sql server2000中指定了默认数据库.加了
Class.forName(drivers).newInstance(); 后还要捕捉异常.麻烦死了!
用配置文件试了试,你上面的程序不做改动一切正常。不知道你具体问题是什么?
不信任用户zqy的登陆.