If I wanted to create a mySql Database using JSP and Java, I would use jsp to execute a compiled java program. Is it possible to execute a compiled C# program with ASP.Net on a Microsoft Server (inetpub/wwwroot)? Given the task to create a mysql database
with ASP.Net and C# (in a separate compiled program) how would you do that? Also, how would you compile the program? And, do you need to place the compiled program in a special folder? Any tutorials on this subject?
Please see example below.
FYI, I will not be using Visual Studio or similar type utility.
RobinHR
Member
2 Points
28 Posts
Create MySql Database with ASP.Net and C#
Jun 02, 2012 04:01 PM|LINK
If I wanted to create a mySql Database using JSP and Java, I would use jsp to execute a compiled java program. Is it possible to execute a compiled C# program with ASP.Net on a Microsoft Server (inetpub/wwwroot)? Given the task to create a mysql database with ASP.Net and C# (in a separate compiled program) how would you do that? Also, how would you compile the program? And, do you need to place the compiled program in a special folder? Any tutorials on this subject?
Please see example below.
FYI, I will not be using Visual Studio or similar type utility.
Thanks,
Robin
*******************************************
adminjava_create_mysql_database.jsp
<%@ page language="java"
import="java.sql.*"
import="java.io.*"
import="java.util.*"
import="javax.servlet.*"
import="jbeans.*"
%>
<HTML>
<HEAD>
<TITLE>Create MySql Database</TITLE>
</HEAD>
<BODY class="adpagecolor" LINK="black" ALINK="black" VLINK="black">
<FONT SIZE="4">
<%-- VARIABLE DECLARATIONS --%>
<%! String strDBName;%>
<%
strDBName = “TestDatabase”;
<FORM name="newdatabaseform"
onSubmit="return validate()"
METHOD="Post"
ACTION="jservlets/MySqlCreateDatabase">
<TABLE BORDER="3" CELLPADDING="1" WIDTH="100%" ALIGN="CENTER">
<TR>
<TD><B>New mySql Database:</B></TD>
<TD><INPUT TYPE="text" NAME="MySqlDatabase" VALUE="<%=strDBName%>" SIZE="65">
</TD>
</TR>
</TABLE>
<P>
<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM>
</FONT>
</BODY>
</HTML>
*************************************************************
MySqlCreateDatabase.java
package jservlets;
import java.io.*;
import java.util.Date;
import java.util.*;
import java.text.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MySqlCreateDatabase extends HttpServlet
{
PrintWriter out;
PrintWriter err;
Connection conn;
Statement stmt;
String strDatabase;
private void getConnection(HttpServletRequest request, HttpServletResponse response)
{
conn = null;
try
{
out.println ("\n\n+++++ getConnection Process +++++\n");
out.println("<P>");
Class.forName("com.mysql.jdbc.Driver").newInstance();
out.println ("JDBC driver loaded");
out.println("<P>");
conn = DriverManager.getConnection ("jdbc:mysql://localhost/mysql", "root", "test");
out.println("mySql Database connection established for mysql");
out.println("<P>");
stmt = conn.createStatement ();
out.println ("Statement created...");
out.println("<P>");
}
catch (ClassNotFoundException cnfe)
{
out.println ("Class Not Found Exception: Could not locate driver" + "<P>") ;
err.println (cnfe.getMessage () ) ;
out.println("<P>");
}
catch (Exception e)
{
out.println ("An unknown error occurred while connecting to the mySql database" + "<P>") ;
err.println (e.getMessage () ) ;
out.println("<P>");
}
}
public void createDatabase(HttpServletRequest request, HttpServletResponse response)
throws SQLException
{
try
{
out.println("<B>Create MySql Database</B>" + "<P>");
String upd = "CREATE DATABASE " + strDatabase;
stmt.executeUpdate(upd);
out.println("MySql Database " + strDatabase + " created." + "<P>");
}
catch (SQLException cnfe)
{
out.println("SQLException: " + "<P>");
err.println (cnfe.getMessage () ) ;
out.println("<P>");
}
catch (Exception e)
{
out.println("Exception: Could not create the database " + strDatabase + "." + "<P>");
err.println (e.getMessage () ) ;
out.println("<P>");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
response.setContentType("text/html");
out = response.getWriter();
err = response.getWriter();
strDatabase = request.getParameter("MySqlDatabase").trim();
getConnection(request, response);
createDatabase(request, response);
}
catch(Throwable e)
{
e.printStackTrace();
}
finally
{
try
{
if (conn != null)
{
conn.close();
}
}
catch(SQLException sqle)
{
out.println("Unable to close database connection.");
err.println (sqle.getMessage () ) ;
out.println("<P>");
}
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}
}