资源描述:
《第12章 java 数据库操作》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、第12章Java数据库操作实验1JDBC-ODBC桥接器2.模板代码Excel.javaimportjava.sql.*;publicclassExcel{publicstaticvoidmain(Stringargs[]){Connectioncon;Statementsql;ResultSetrs;try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}catch(ClassNotFoundExceptione){System.out.println(""+
2、e);}try{con=DriverManager.getConnection("jdbc:odbc:star","","");sql=con.createStatement();rs=sql.executeQuery("SELECT*FROMmessage");while(rs.next()){Stringnumber=rs.getString(1);Stringname=rs.getString(2);Stringunit=rs.getString(3);doubleprice=rs.getInt(4)
3、;System.out.println(number+","+name+","+unit+","+price);}con.close();}catch(SQLExceptione){System.out.println(e);}}}实验2查询、更新和插入操作2.模板代码LookCar.javaimportjava.sql.*;importjava.util.List;publicclassLookCar{publicstaticvoidmain(Stringargs[]){Connectioncon;Sta
4、tementsql;ResultSetrs;try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}catch(ClassNotFoundExceptione){System.out.println(""+e);}try{con=DriverManager.getConnection("jdbc:odbc:mycar","","");sql=con.createStatement();rs=sql.executeQuery("INSERTINTOlistVALU
5、ES('001','奔驰',390000,'2008-12-20')");//向list表插入记录:('001','奔驰',390000,'2008-12-20')rs=sql.executeQuery("INSERTINTOlistVALUES('002','奥迪',320000,'2009-07-20')");//向list表插入记录:('002','奥迪',320000,'2009-07-20')rs=sql.executeQuery("INSERTINTOlistVALUES('003','宝马',
6、350000,'2009-10-10')");//向list表插入记录:('003','宝马',350000,'2009-10-10')rs=sql.executeQuery("SELECT*FROMlist");while(rs.next()){Stringnumber=rs.getString(1);Stringname=rs.getString(2);doubleprice=rs.getDouble(3);DatemadeDate=rs.getDate(4);System.out.println(nu
7、mber+","+name+","+price+","+madeDate);}rs=sql.executeQuery("UPDATElistSETprice=380000WHEREnumber='001'");//将list表中number是001的price的值更新为380000rs=sql.executeQuery("SELECT*FROMlistWHEREnumber='001'");//查询number为001的记录booleanboo=rs.next();if(boo){Stringnumber=
8、rs.getString(1);Stringname=rs.getString(2);doubleprice=rs.getDouble(3);DatemadeDate=rs.getDate(4);System.out.println(number+","+name+","+price+","+madeDate);}con.close();}catch(SQLExceptione){System.out.print