Java JDBC 操作(sql server, 源码)

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();
			}
		}
		
	}
	
	
	
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值