用sqlserver建立了空数据库studentmanager ODBC数据源设置的名字也是studentmanager属性文件配置:
driver.properties
drivers=com.microsoft.jdbc.sqlserver.SQLServerDriver
url=jdbc:microsoft:sqlserver://localhost:1433;
DatabaseName=studentmanager
user=sa
password=""注:和 类CreatTableTest 放在同一个目录下。源代码如下:import java.sql.*;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;public class CreatTableTest {
private Connection con;
private String url;
private String userName;
private String password;
public static void main(String args[]){
CreatTableTest test=new CreatTableTest();
test.getProperty();
Connection con=test.getConnection();
test.creatTable(con);
test.getStudent(con);
}
public void creatTable(Connection con){
try{
Statement st=con.createStatement();
String sql="CREATE TABLE student(姓名 varch(12)NOT NULL,"+"学号 varchar(10) NOT NULL,"+"出生日期 datetime NOT NULL,专业 varchar(10)NULL,"+"籍贯 varchar(30) NULL)";
System.out.println("输出的SQL语句是:");
System.out.println(sql);
sql="insert into student values('王成','20021022','1977-6-10','英语','河北')";
st.executeUpdate(sql);
st.close();
}catch(SQLException e){
e.printStackTrace();
}
}
public void getStudent(Connection con){
try{
Statement st=con.createStatement();
String sql="select * from student";
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
String name=rs.getString("姓名");
String number=rs.getString("学号");
Date date=rs.getDate("出生日期");
String spe=rs.getString("专业");
String address=rs.getString("籍贯");
System.out.println("\n姓名 "+name+"\t学号 "+number+"\t出生日期 "+date+"\t专业 "+spe+"\t籍贯 "+address);
}
st.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
public Connection getConnection(){
try{
con=DriverManager.getConnection(url,userName,password);
}catch(SQLException e){
e.printStackTrace();
}
return con;
}
public void getProperty(){
Properties prop=new Properties();
try{
FileInputStream in=new FileInputStream("driver.properties");
prop.load(in);
String driver=prop.getProperty("drivers");
if(driver !=null){
System.setProperty("jdbc:drivers",driver);
url=prop.getProperty("url");
userName=prop.getProperty("user");
password=prop.getProperty("password");
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}编译出现:
java.io.FileNotFoundException: driver.properties (系统找不到指定的文件。)
java.sql.SQLException: The url cannot be null
java.lang.NullPointerException
Exception in thread "main" 帮忙解决下,谢谢。
driver.properties
drivers=com.microsoft.jdbc.sqlserver.SQLServerDriver
url=jdbc:microsoft:sqlserver://localhost:1433;
DatabaseName=studentmanager
user=sa
password=""注:和 类CreatTableTest 放在同一个目录下。源代码如下:import java.sql.*;
import java.util.Properties;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;public class CreatTableTest {
private Connection con;
private String url;
private String userName;
private String password;
public static void main(String args[]){
CreatTableTest test=new CreatTableTest();
test.getProperty();
Connection con=test.getConnection();
test.creatTable(con);
test.getStudent(con);
}
public void creatTable(Connection con){
try{
Statement st=con.createStatement();
String sql="CREATE TABLE student(姓名 varch(12)NOT NULL,"+"学号 varchar(10) NOT NULL,"+"出生日期 datetime NOT NULL,专业 varchar(10)NULL,"+"籍贯 varchar(30) NULL)";
System.out.println("输出的SQL语句是:");
System.out.println(sql);
sql="insert into student values('王成','20021022','1977-6-10','英语','河北')";
st.executeUpdate(sql);
st.close();
}catch(SQLException e){
e.printStackTrace();
}
}
public void getStudent(Connection con){
try{
Statement st=con.createStatement();
String sql="select * from student";
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
String name=rs.getString("姓名");
String number=rs.getString("学号");
Date date=rs.getDate("出生日期");
String spe=rs.getString("专业");
String address=rs.getString("籍贯");
System.out.println("\n姓名 "+name+"\t学号 "+number+"\t出生日期 "+date+"\t专业 "+spe+"\t籍贯 "+address);
}
st.close();
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
public Connection getConnection(){
try{
con=DriverManager.getConnection(url,userName,password);
}catch(SQLException e){
e.printStackTrace();
}
return con;
}
public void getProperty(){
Properties prop=new Properties();
try{
FileInputStream in=new FileInputStream("driver.properties");
prop.load(in);
String driver=prop.getProperty("drivers");
if(driver !=null){
System.setProperty("jdbc:drivers",driver);
url=prop.getProperty("url");
userName=prop.getProperty("user");
password=prop.getProperty("password");
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}编译出现:
java.io.FileNotFoundException: driver.properties (系统找不到指定的文件。)
java.sql.SQLException: The url cannot be null
java.lang.NullPointerException
Exception in thread "main" 帮忙解决下,谢谢。
你的配置文件driver.properties放位置不对 要放在相对所在类的classpath的根部
如果你现在的类所在的包路径为 com.csdn.jdbc.test 那么文件应该放在 com文件夹的上一级目录这个怎么放的哦?不会啊,还请指教
运行的时候才会去读这个properties
丰搜网 专搜计算机知识的地方Web,Photoshop,Dreamweaver, Frontpage,Css,Js,视频,短信sms,彩信MMS,WAP,3G,博客,软件工程,项目管理,让我们关注更多的计算机领域的知识,共享资源,携手共进。http://www.sosof.com