关于JDBC的程序,谁能帮小弟解决一下问题,谢谢~!
import java.sql.SQLException;import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.Date;
import java.util.Properties;
import
import
import
public class CreatTableTest
{
private Connection con;
String URL ;
String userName = "";
String password = "";
public static void main(String [] args)
{
CreatTableTest test = new CreatTableTest();
test.getProperty();
Connection con = test.getConnection();
test.createTable(con);
test.getStudent(con);
}
public void createTable(Connection con)
{
try
{
Statement st = con.createStatement();
String sql = "CREATE TABLE student(姓名 carchar(12) NOT NULL, 学号 varchar(10) NOT NULL, " +
"出生日期 datetime NOT NULL,专业 varchar(10) NOT NULL,籍贯 carchar(30) NULL)";
System.out.println("输出的SQL语句是:");
System.out.println(sql);
st.execute(sql);
sql = "insert into student values('俊峰','20021022','1984-01-13','英语','山西太原')";
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 mber = rs.getString(1);
String name = rs.getString("姓名");
String Number = rs.getString("学号");
Date tt = rs.getDate("出生日期");
String spe = rs.getString("专业");
String address = rs.getString("籍贯");
System.out.println("\n姓名"+name + "\t学号" + Number + "\t出生日期" + tt + "\t专业" + spe + "\t籍贯" + address);
}
st.close();
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public Connection getConnection()
{
try
{
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(URL,userName,password);
}
/*catch(ClassNotFoundException ex )
{
ex.printStackTrace();
}*/
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 drivers = prop.getProperty("drivers");
if(drivers != null)
System.setProperty("jdbc.drivers",drivers);
URL = prop.getProperty("url");
userName = prop.getProperty("user");
password = prop.getProperty("password");
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
这个程序出现的错误是:
Driver.properties (系统找不到指定的文件。)
at (Native Method)
at (FileInputStream.java:106)
at (FileInputStream.java:66)
at CreatTableTest.getProperty(CreatTableTest.java:98)
at CreatTableTest.main(CreatTableTest.java:20)
java.sql.SQLException: The url cannot be null
at java.sql.DriverManager.getConnection(DriverManager.java:489)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at CreatTableTest.getConnection(CreatTableTest.java:79)
at CreatTableTest.main(CreatTableTest.java:21)
Exception in thread "main" java.lang.NullPointerException
at CreatTableTest.createTable(CreatTableTest.java:30)
at CreatTableTest.main(CreatTableTest.java:23)
什么地方出现了错误,那位大哥能帮我解决一下,小弟万分感激!