微前端架构如何改变企业的开发模式与效率提升
582
2022-10-07
在Myeclipse下servlet和oracle配置和连接
在Myeclipse下servlet和oracle配置和连接 1.导入的包servlet-api.jar和ojdbc6.jar 2.必要代码:Jdbc连接代码、TestHtml页面,TestOracle代码,web.xml配置文件 3.建立一个数据库myUser表, ****************************************** 1.连接Jdbc的代码
package eduask; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class MyJdbcTools { /**用户名*/ static String user="scott"; /**密码*/ static String password="tiger"; /**驱动*/ static String driverName="oracle.jdbc.driver.OracleDriver"; static String url="jdbc:Oracle:thin:@localhost:1521/orcl"; /** 获取一个连接 */ public static Connection getConnection() { Connection c = null; try { Class.forName(driverName);// 加载驱动 c = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return c; } /** 关闭数据库 */ public static void close(Connection c) { if (c != null) { try { if (!c.isClosed()) {// 如果没有关闭 c.close(); } } catch (SQLException e) { e.printStackTrace(); } } } }
2.TestHtml的代码(显示网页的代码)
1.package eduask; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.import javax.servlet.import javax.servlet.//测试在网页显示HTML代码; public class TestHtml extends HttpServlet{ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter out=resp.getWriter(); out.println("
"); } }3.TestOracle的代码(获得网页的代码,放入到数据库中。)
package eduask; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import javax.servlet.ServletException; import javax.servlet.import javax.servlet.import javax.servlet.public class TestOracle extends HttpServlet{ @Override protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { // PrintWriter out=res.getWriter(); // out.write("RequestURI:"+req.getRequestURI()+"\n"); // out.write("RequestURL:"+req.getRequestURL().toString()+"\n"); // out.write("username:"+req.getParameter("username")+"\n"); // out.write("password:"+req.getParameter("password")+"\n"); String id=req.getParameter("id"); String name=req.getParameter("name"); Connection conn=null; PreparedStatement ps=null; try { conn=MyJdbcTools.getConnection(); String sql="insert into myUser(id,name)values(?,?)"; ps=conn.prepareStatement(sql); ps.setString(1, id); ps.setString(2,name); ps.execute(); } catch (Exception e) { e.getStackTrace(); } } }
2.web.xml文件的配置
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~