一、JDK的安装步骤 1、下载JDK:www.sun.com, 2、双击安装,然后next,next,next。。。 3、配置环境变量:在windows操作系统中配置path和classpath两个环境变量; JAVA_HOME=D:Program Files (x86)JavaJDK Path=%JAVA_HOME%in;%JAVA_HOME%jrein; CLASSPATH=.;%JAVA_HOME%libdt.jar;%JAVA_HOME%lib ools.jar; 测试:java -version 4、编译:javac Hello.java 执行:java Hello 二、变量的命名规范 1、变量命名语法规范 1、应该以字母、下划线或者美元符开头 2、后面跟字母、下划线、美元符或者数字 3、java变量名没有长度限制; 4、java变量名对大小写敏感 2、驼峰命名法: 1、变量名应该用有意义的英文单词; 2、变量名如果只有一个单词,则所有的字母小写; 3、变量名如果由多个英文单词组成,则从第二个单词开始首字母大写; 三、java连接mysql try { //1.注册驱动 //Class.forName("com.mysql.jdbc.Driver"); DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //2.建立连接 Connection conn=(Connection) DriverManager.getConnection ("jdbc:mysql://localhost:3306/jdbc","root",""); //3.创建语句 Statement st=(Statement) conn.createStatement(); //4.执行语句 ResultSet rs=st.executeQuery("select * from user"); //5.处理结果 while(rs.next()){ System.out.println(rs.getObject(1)+" "+rs.getObject (2)+" "+rs.getObject (3)+" "+rs.getObject(4)); } //6.释放资源 rs.close(); st.close(); conn.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } 四、//演示如何使用JDBC_ODBC桥连接方式:java连接oracle数据库 try { //1.加载驱动 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //2.得到连接 Connection ct=DriverManager.getConnection ("jdbc:odbc:testsp","scott","700513"); //从下面开始和sqlserver一模一样 Statement sm=ct.createStatement(); ResultSet rs=sm.executeQuery("select * from emp"); while(rs.next()){ //用户名 System.out.println("用户名 :"+rs.getString(2)); } } catch (Exception e) { e.printStackTrace(); } 五、java连接oracle数据库---第二中方式 try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection ct=DriverManager.getConnection ("jdbc:oracle:thin:@127.0.0.1:1251:oral","scott","700513"); Statement sm=ct.createStatement(); //从SCOTT的sal中减去100 sm.executeUpdate("update ump set sal=sal-100 where ename='SCOTT'"); //给SMITH加100 int i=7/0; sm.executeUpdate("update emp set sal=sal+100 where ename='SMITH'"); //关闭资源 sm.close(); ct.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } 六、java--oracle分页查询分页程序 PreparedStatement ps=null; Connection ct=null; ResultSet rs=null; Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
Connection conn = DriverManager.getConnection(url,"scott","700513");
Statement stmt = conn.createStatement(); //接收pageNow String s_pageNow=(String)request.getParameter("pageNow"); int pageNow=1; if(s_pageNow!=null){ pageNow=Integer.parseInt(s_pageNow); } //查询总页数 int pageCount=0; int rowCount=0;//共有几条记录 int pageSize=3;//每页显示几条记录 rs=stmt.executeQuery("select count(*) from emp"); if(rs.next()){ rowCount=rs.getInt(1); if(rowCount%pageSize==0){ pageCount=rowCount/pageSize; }else{ pageCount=rowCount/pageSize+1; } }
rs = stmt.executeQuery("select * from (select a1.*,rownum rn from (select * from emp) a1 where rownum<="+pageNow*pageSize+") where rn>="+((pageNow-1)*pageSize+1)+"");
while(rs.next()){ out.println("<tr>"); out.print("<td>"+rs.getString(2)+"</td>"); out.println("<td>"+rs.getString(6)+"</td>"); out.println("</tr>"); }
//打印总页数 for(int i=1;i<=pageCount;i++){ out.print("<a href=Mytest.jsp?pageNow="+i+"> ["+i+"] </a>"); } rs.close();
stmt.close();
conn.close();