1、配置好SQL Server (账号sa,密码123456,数据库名database1,表名MobilePhone)
2、导入SQL Server的驱动程序,下载连接如下:http://download.csdn.net/download/m0_37307670/10191711
3、进行代码操作:源码如下
package java2exam;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Database {
public static void main(String[] args) {
DataBaseHelper dbHelper = new DataBaseHelper();
// System.out.println("查询测试");
// dbHelper.queryTest();
//
// System.out.println("插入测试");
// dbHelper.insertTest();
// dbHelper.queryTest();
//
// System.out.println("更新测试");
// dbHelper.updateTest();
// dbHelper.queryTest();
//
// System.out.println("删除测试");
// dbHelper.deleteTest();
// dbHelper.queryTest();
System.out.println("preparedStatement测试");
dbHelper.preparedStatementTest();
dbHelper.closeConnection(); //关闭连接
}
}
class DataBaseHelper{
private static String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url = "jdbc:sqlserver://DESKTOPL\\SQLEXPRESS:1433;DatabaseName=database1";
private static String username = "sa";
private static String password = "123456";
private Connection con;
private Statement sql;
public DataBaseHelper() {
intial();
getConnection();
}
private void intial() {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
System.out.println("驱动加载失败");
}
}
private void getConnection() {
try {
con = DriverManager.getConnection(url, username, password);
System.out.println("连接成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 查询并打印出MobilePhone表中的所有信息
*/
public void queryTest() {
try {
sql = con.createStatement();
ResultSet rs = sql.executeQuery("select mpbrand, mptype, price from MobilePhone"); //这个不是真正的set
while (rs.next()) {
String str1 = rs.getString(1);
System.out.println(str1);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void insertTest() {
try {
sql = con.createStatement();
int line = sql.executeUpdate("insert into MobilePhone(mpid, mpbrand, mptype, price) values(6, '1', '2', '3')");
System.out.println("插入的行数:" + line);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void deleteTest() {
try {
sql = con.createStatement();
int line = sql.executeUpdate("delete from MobilePhone where mpbrand = 'test'");
System.out.println("删除的行数:" + line);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateTest() {
try {
sql = con.createStatement();
int line = sql.executeUpdate("update MobilePhone set mpbrand = 'test' where mpbrand = '1'");
System.out.println("更新的行数:" + line);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void preparedStatementTest() {
PreparedStatement sql = null;
try {
sql = con.prepareStatement("select mpbrand, mptype, price from MobilePhone where price > ?");
sql.setInt(1, 3000);
ResultSet rs = sql.executeQuery();
while (rs.next()) {
String str1 = rs.getString(1);
System.out.println(str1);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
void closeConnection() {
if (sql != null) {
try {
sql.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
System.out.println("关闭成功");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}