Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save swapnil-webonise/6791742 to your computer and use it in GitHub Desktop.
Save swapnil-webonise/6791742 to your computer and use it in GitHub Desktop.
Creating MYSQL Database Connection Using Java Servlet
package examadmin;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ExamConnection
{
private static Connection con = null;
ExamConnection()
{
}
private static void createConnection()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://172.16.59.202/test";
con=DriverManager.getConnection(url,"root","root");
System.out.println("connection created");
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
System.out.println("ClassNotFoundException"+e);
}
}
public static Connection getConnection()
{
if(con == null)
{
createConnection();
}
return con;
}
public static void main(String[] argv)
{
Connection con1 = ExamConnection.getConnection();
System.out.println(con1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment