数据库操作:java连接MSSQL实例代码演示
sql server 2005或2008
package com.qiaole.conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Mssql2008 {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
.newInstance();
String url = "jdbc:sqlserver://localhost:1433;databasename=joeBank";
Connection conn = DriverManager.getConnection(url, "sa", "qiaole");
String sql = "select * from T_Person";
PreparedStatement psStatement = conn.prepareStatement(sql);
ResultSet rSet = psStatement.executeQuery();
System.out.println("\tMssql查询数据");
while (rSet.next()) {
System.out.println(rSet.getString(1) + "\t" + rSet.getInt(2)
+ "\t" + rSet.getString(3));
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
}
}
需要添加的包: