jdbc

package jdbctest1;
//import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.*;
//import java.lang.*;


public class DbConnect {
	public static void main(String[] args) {
		Connection conn = getConnection("sa", "971120");
		//System.out.println("Please enter the operation you want to perform:");
		System.out.println("Please enter the SQL statement you want to execute:");
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String Sql = null;
		try {
			Sql = br.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		char[] S = 	 Sql.toCharArray();
		switch (S[0]) {
		case 's':
			query(conn,Sql);
			break;
		case 'i':
			insert(conn,Sql);
			break;
		case 'u':
			update(conn,Sql);
			break;
		case 'd':
			delete(conn,Sql);
			break;
		default:
			System.out.println("you entered the wrong statement");
			break;
		}
		releaseConnection(conn);
	}
	
	//数据库连接
	public static Connection getConnection(String userName, String password){
		Connection conn = null;//声明连接对象
		String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Student";
		try {
			Class.forName(driver);//注册(加载)驱动程序
			conn = DriverManager.getConnection(url, userName, password);//获得数据库连接
			System.out.println("connect succeed!");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("connect failed");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("connect failed");
		}
		return conn;
	}
	
	//查询数据
	public static void query(Connection conn, String Sql){
		//String Sql = "select * from SC";
		Statement stmt;
		try {
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(Sql);//执行sql语句并返还结束
			
			while(rs.next()){
				System.out.println("sno:" + rs.getString("sno") + " cno:" + rs.getString("cno") + " grade:" + rs.getString("grade"));
			}
			
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(stmt != null){
				try {
					stmt.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	
	//插入数据
	public static void insert(Connection conn, String Sql){
		//String Sql = "insert into SC values('95002',4,89)";
		try {
			Statement stmt = conn.createStatement();
			int count = stmt.executeUpdate(Sql);
			System.out.println("you have successfully inserted " + count + " data into the table");
			if(stmt != null){
				try {
					stmt.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//更新数据
	public static void update(Connection conn,String Sql){
		//String Sql = "update SC set cno = '7' where grade = '89'";
		try {
			Statement stmt = conn.createStatement();
			stmt.executeUpdate(Sql);
			System.out.println("update succeed");
			if(stmt != null){
				try {
					stmt.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//删除数据
	public static void delete(Connection conn, String Sql){
		//String Sql = "delete from SC where sno = '95004'";
		try {
			Statement stmt = conn.createStatement();
			stmt.executeUpdate(Sql);
			System.out.println("delete succeed");
			if(stmt != null){
				try {
					stmt.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//释放数据库连接
	public static void releaseConnection(Connection conn){
		if(conn != null)
			try {
				conn.close();
				System.out.println("release succeed!");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值