我有一个java类sample.java和网页example.jsp。我希望运行java类并从jsp读取值。这意味着用户不会运行jsp,她只运行java类。查找以下代码:
String mmobile = mobile;
//Prepare Url
URLConnection myURLConnection=null;
URL myURL=null;
BufferedReader reader=null;
String mainUrl="http://localhost:8080/Portal/example.jsp?";
//Prepare parameter string
StringBuilder sbPostData= new StringBuilder(mainUrl);
sbPostData.append("mobile=").append(mmobile);
//final string
mainUrl = sbPostData.toString();
try
{
//prepare connection
myURL = new URL(mainUrl);
myURLConnection = myURL.openConnection();
myURLConnection.connect();
}
catch (IOException e)
下面给出了example.jsp代码:
<%@page import="java.sql.*"
%>
<%@ page import="myclass.SAMPLE" %>
<%
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = null;
String mphone = request.getParameter("mobile");
int mcompregkeyid=0;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/myschema","myuser","mypass");
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("Select max(keyid) from mytable");
rset.next();
mcompregkeyid=rset.getInt(1);
} finally {
if (conn != null) try { conn.close(); } catch (SQLException e) {
out.println("(\"Could not connect to local server.\nPlease try after some time.!!!\")");
}
}
SAMPLE.getValue(String.valueOf(mcompregkeyid));
%>
1条答案
按热度按时间uwopmtnx1#
为什么要在jsp文件中编写jdbc数据库连接代码?
请遵守mvc设计模式标准。