Although the study javaweb time is not long, also did some small projects. However, I still often forget about connecting to the database code, always need to go over the notes. Today you want to save some notes, if necessary later, you can find here.
Code:
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
public class Jdbctools {
private static String driver= "Com.mysql.jdbc.Driver";
Oracle is: "Oracle.jdbc.driver.OracleDriver"
SQL Server is: "Com.microsoft.sqlserver.jdbc.SQLServerDriver"
private static String url= "Jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=utf8";
Oracle: "Jdbc:oracle:thin: @localhost: 1521:orcl"
SQL Server is: "jdbc:sqlserver://localhost:1433;database= database Name"
private static String username= "root";
private static String password= "sleeping";
public static Connection getconnection () {
Connection Conn=null;
try {
Class.forName (driver);
Conn=drivermanager.getconnection (Url,username,password);
System.out.println ("connected successfully");
catch (Exception e) {
System.out.println ("Connection failed");
TODO auto-generated Catch block
E.printstacktrace ();
}
Return conn;
}
public static void Release (ResultSet rs,preparedstatement pstmt,connection conn) {
try {
if (rs!=null)
{Rs.close ();} else if (pstmt!=null)
{Pstmt.close ();} else if (conn!=null)
{Conn.close ();}
catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}