問題:
給出一個完整的實例,包括建立所需要的用戶數(shù)據(jù)庫,配置ODBC數(shù)據(jù)源,編寫訪問數(shù)據(jù)庫的程序,查看于運行結(jié)果。
其中訪問數(shù)據(jù)庫程序輸出班級為“025”的記錄,并將“025”改為“計算機”。
package 實例數(shù)據(jù)庫;import java.sql.*;public class dataBase {public static void main(String[] args) {//聲明變量Statement stmt;PReparedStatement pstmt;ResultSet rs;try {//加載驅(qū)動程序Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//連接數(shù)據(jù)庫String url="jdbc:odbc:STU";Connection con=DriverManager.getConnection(url, "", "");stmt=con.createStatement();//執(zhí)行查詢操作String sql1="SELECT 學(xué)號,姓名,班級 FROM STU WHERE 班級='025'";rs=stmt.executeQuery(sql1);//顯示查詢返回的結(jié)果while(rs.next()){String str1=rs.getString("學(xué)號");String str2=rs.getString("姓名");System.out.println(str1+str2);}String sql2="UPDATE STU SET 班級=? WHERE 班級=?";pstmt=con.prepareStatement(sql2);pstmt.setString(1, "計算機");pstmt.setString(2, "025");pstmt.executeUpdate();con.close();} catch (ClassNotFoundException e) {// TODO 自動生成的 catch 塊e.printStackTrace();} catch (SQLException e) {// TODO 自動生成的 catch 塊e.printStackTrace();}}}
新聞熱點
疑難解答
圖片精選