1. 程式人生 > >Jdbc對Mysql資料庫的基本操作

Jdbc對Mysql資料庫的基本操作

jdbc對資料庫的查詢(無參):
<span style="font-size:14px;">	public void getQueryNoargAll(){
		JdbcMysqlpool jdbc = new JdbcMysqlpool();
		Connection con = null;
		Statement st = null;
		ResultSet rlt = null;
		String sql;
		try {
			//獲取資料庫連線
			con = jdbc.getConnection();
			//在連線裡開啟一條通道createStatement,返回Statement物件
			st = con.createStatement();
			sql="select *from student";
			//把我們想要的結果sql語句發給資料庫,資料庫返回的資料用java ResultSet來接受。
			rlt = st.executeQuery(sql);
			while (rlt.next()) {
				String name=rlt.getString(1);
				String interest = rlt.getString(2);
				System.out.println("name:"+name+";interest:"+interest);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(rlt!=null)
					rlt.close();
				if(st!=null)
					st.close();
				if(con!=null)
					con.close();
			} catch (Exception e2) {
			}
		}
	}</span>

jdbc對資料庫的查詢(有參):
<span style="font-size:14px;">	public void getQueryArg(String interest){
		JdbcMysqlpool jdbc = new JdbcMysqlpool();
		Connection con = null;
		PreparedStatement pst = null;
		ResultSet re = null;
		String sql;
		try {
			//建立連線
			con = jdbc.getConnection();
			sql = "select name from student where interest=?";
			//帶引數的sql傳入資料庫裡</span>
<span style="font-size:14px;"><span style="white-space:pre">			</span>//在這裡需要注意我們是有引數時,呼叫的是</span><span style="font-size: 14px; font-family: Arial, Helvetica, sans-serif;">prepareStatement</span><span style="font-size:14px;">
			pst = con.prepareStatement(sql);
			//設定引數的值
			pst.setString(1, interest);
			//執行查詢,返回ResultSet結果集
			re = pst.executeQuery();
			while (re.next()) {
				System.out.println(interest+"特別興趣愛好的哥們"+re.getString("name"));
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(re!=null)
					re.close();
				if(con!=null)
					con.close();
				if(pst!=null)
					pst.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}</span>
jdbc呼叫資料庫的系統函式(有參):
<span style="font-size:14px;">	//獲取有參的系統函式
	public void getFunctionSysarg(String abc){
		JdbcMysqlpool jdbc = new JdbcMysqlpool();
		CallableStatement cst = null;
		Connection con = null;
		ResultSet re = null;
		String sql;
		try {
			con = jdbc.getConnection();</span>
<span style="font-size:14px;"><span style="white-space:pre">			</span>//此函式的作用是將傳進來的字串全部變成大寫
			sql = "select UPPER(?)";
		<span style="white-space:pre">	</span>//建立一個用於呼叫儲存過程(函式)的prepareCall物件,生成CallableStatement物件,並把SQL傳進去。
			cst=con.prepareCall(sql);
			cst.setString(1, abc);
			//執行SQL,返回Resultset結果集
			re=cst.executeQuery();
			//查詢結果集
			re.next();
			//列印結果集
			System.out.println(re.getString(1));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}</span>

jdbc呼叫資料庫的自定義函式(有參):

	public void getFuntion(int a,int b){
		JdbcMysqlpool jdbc = new JdbcMysqlpool();
		CallableStatement cst = null;
		Connection con = null;
		ResultSet re = null;
		String sql;
		try {
			con = jdbc.getConnection();
			sql = " select xiemnf_add(?,?)";
<span style="white-space:pre">			</span>//建立一個用於呼叫儲存過程(函式)的prepareCall物件,生成CallableStatement物件,並把SQL傳進去。
			cst=con.prepareCall(sql);
			cst.setInt(1, a);
			cst.setInt(2, b);
<span style="white-space:pre">			//執行SQL,返回Resultset結果集</span>
			re=cst.executeQuery();
			re.next();
			System.out.println(re.getInt(1));
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			
				try {
					if(con!=null)
						con.close();
					if(cst!=null)
						cst.close();
					if(re!=null)
					re.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
	}


jdbc呼叫資料庫的自定義過程(有參)
<span style="font-size:14px;">	public void getProcedure(int a,int b){
		JdbcMysqlpool jdbc = new JdbcMysqlpool();
		Connection con = null;
		CallableStatement cst = null;
		String sql;
		try {
			con = jdbc.getConnection();
			sql = "{call xiemnp_mul(?,?,?,?)}";
			//建立一個用於呼叫儲存過程(函式)的prepareCall物件,生成CallableStatement物件,並把SQL傳進去。
			cst = con.prepareCall(sql);
			cst.setInt(1, a);
			cst.setInt(2, b);
			</span>
<span style="font-size:14px;"><span style="white-space:pre">			</span>//設定sql返回的資料型別
			cst.registerOutParameter(3, java.sql.Types.INTEGER);
			cst.registerOutParameter(4, java.sql.Types.INTEGER);
			cst.execute();
			//執行SQL,返回Resultset結果集
			
			//列印結果集
			System.out.println(cst.getInt(3)+cst.getInt(4));
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				if(con!=null)
					con.close();
				if(cst!=null)
					cst.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		
	}</span>