Created
October 2, 2013 10:30
-
-
Save swapnil-webonise/6791742 to your computer and use it in GitHub Desktop.
Creating MYSQL Database Connection Using Java Servlet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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