java sqlserver 2000_谁能救救我啊,关于JAVA连接SQLserver2000
原代碼:大量import省略,這里應該沒錯,因為不夠篇幅importjavapublicclassCreateTableTest{privateConnectioncon;privateStringurl;privateStringuser;privateStringpassword;publicst...
原代碼:大量import省略,這里應該沒錯,因為不夠篇幅
import java
public class CreateTableTest {
private Connection con;
private String url;
private String user;
private String password;
public static void main(String[] args)
{
CreateTableTest test = new CreateTableTest();
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(studentName varchar(12) NOT NULL," + "studentNumber varchar(10) NOT NULL," + "studentBirthday datetime NOT NULL , mainObject varchar(10) NULL," + "studentAddress varchar(30) NULL)";
System.out.println("輸出的SQL語句是:");
System.out.println(sql);
st.execute(sql);
sql = "insert into student values ('王成' , '200610091121' , '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("studentName");
String number = rs.getString("studentNumber");
Date date = rs.getDate("studentBirthday");
String spe = rs.getString("mainObject");
String address = rs.getString("studentAddress");
System.out.println("\nstudentName: " + name + "\tstudentNumber: " + number + "\tstudentBirthday: " + date + "\tmainObject" + spe + "\tstudentAddress: " + address);
}
st.close();
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public Connection getConnection()
{
try
{
con = DriverManager.getConnection(url, user, 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");
user = prop.getProperty("user");
password = prop.getProperty("password");
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
屬性文件的配置 driver.properties
drivers=com.microsoft.jdbc.sqlserver.SQLServerDriver
url=jdbc:microsoft:sqlserver://localhost:1433;
DatabaseName=studentmanager;
user=sa;
password=KALASHI3321;
能編譯但不能執行報錯:
--------------------Configuration: --------------------
java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at CreateTableTest.getConnection(CreateTableTest.java:72)
at CreateTableTest.main(CreateTableTest.java:21)
Exception in thread "main" java.lang.NullPointerException
at CreateTableTest.createTable(CreateTableTest.java:29)
at CreateTableTest.main(CreateTableTest.java:22)
Process completed.
展開
總結
以上是生活随笔為你收集整理的java sqlserver 2000_谁能救救我啊,关于JAVA连接SQLserver2000的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学mysql是学指令吗_学习Mysql
- 下一篇: java 脚本引擎性能_Java SE