DriverManager是负责选择数据库驱动程序和建立新数据库连接的类
但在驱动程序管理器能激活驱动程序之前,必须要进行注册
注册方法有两种 一种是Class.forName("")(成功)
另一种是用命令行参数来设定jdbc.drivers属性
如:java -Djdbc.drivers=COM.microsoft.sqlserver.jdbc.SQLServerDriver Greetings
命令行参数这个我一直不成功
请大家帮我看看我是哪里不对了
import java.sql.*;
import java.io.*;
import java.util.*;
class TestDB
{
public static void main (String args[])
{
try
{
Connection conn = getConnection();
Statement stat = conn.createStatement(); stat.execute("CREATE TABLE Greetings (Name CHAR(20))");
stat.execute(
"INSERT INTO Greetings VALUES ('Hello, World!')"); ResultSet result
= stat.executeQuery("SELECT * FROM Greetings");
result.next();
System.out.println(result.getString(1));
result.close(); stat.execute("DROP TABLE Greetings");
stat.close();
conn.close();
}
catch (SQLException ex)
{
while (ex != null)
{
ex.printStackTrace();
ex = ex.getNextException();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
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.properties");
if (drivers != null)
System.setProperty("jdbc.properties",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);
}
}
但在驱动程序管理器能激活驱动程序之前,必须要进行注册
注册方法有两种 一种是Class.forName("")(成功)
另一种是用命令行参数来设定jdbc.drivers属性
如:java -Djdbc.drivers=COM.microsoft.sqlserver.jdbc.SQLServerDriver Greetings
命令行参数这个我一直不成功
请大家帮我看看我是哪里不对了
import java.sql.*;
import java.io.*;
import java.util.*;
class TestDB
{
public static void main (String args[])
{
try
{
Connection conn = getConnection();
Statement stat = conn.createStatement(); stat.execute("CREATE TABLE Greetings (Name CHAR(20))");
stat.execute(
"INSERT INTO Greetings VALUES ('Hello, World!')"); ResultSet result
= stat.executeQuery("SELECT * FROM Greetings");
result.next();
System.out.println(result.getString(1));
result.close(); stat.execute("DROP TABLE Greetings");
stat.close();
conn.close();
}
catch (SQLException ex)
{
while (ex != null)
{
ex.printStackTrace();
ex = ex.getNextException();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
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.properties");
if (drivers != null)
System.setProperty("jdbc.properties",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);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货