Package foo;
Import java.sql.*;
public class Jdbcdemo {
private static Connection Conn;
private static Statement PS;
private static ResultSet Rs;
private static final String DRIVER = "Com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://xx.xx.xx.xx:port/software_app?user=software_app&password= SOFTWARE&CHARACTERENCODING=GBK ";
private static final String USER = "xxx";
private static final String PASS = "xxx";
Public Jdbcdemo () {
Jdbcdemo.getconnection ();
}
public static Connection getconnection () {
System.out.println ("In-connection ...");
try {
try {
Class.forName (Jdbcdemo.driver). newinstance ();
} catch (Instantiationexception e) {
E.printstacktrace ();
} catch (Illegalaccessexception e) {
E.printstacktrace ();
}
conn = Drivermanager.getconnection (Jdbcdemo.url);
SYSTEM.OUT.PRINTLN ("successful Connection");
}catch (SQLException e) {
E.printstacktrace ();
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}
Return conn;
}
public static Statement getstatement (String sql) {
SYSTEM.OUT.PRINTLN ("Execute SQL statement ...");
try {
PS = Conn.createstatement (resultset.type_scroll_sensitive, resultset.concur_updatable);
if (sql.substring (0, 6). Equals ("select")) {
rs = ps.executequery (SQL);
SYSTEM.OUT.PRINTLN ("Completed query operation, result returned resultset collection");
}else if (sql.substring (0, 6). Equals ("delete")) {
Ps.executeupdate (SQL);
System.out.println ("Deleted operation completed");
}else if (sql.substring (0, 6). Equals ("Insert")) {
Ps.executeupdate (SQL);
SYSTEM.OUT.PRINTLN ("Increased operation completed");
}else{
Ps.executeupdate (SQL);
SYSTEM.OUT.PRINTLN ("Completed update operation");
}
}catch (SQLException e) {
E.printstacktrace ();
}
return PS;
}
public static ResultSet Getresultset () {
SYSTEM.OUT.PRINTLN ("Query results are:");
Return RS;
}
public static void CloseConnection () {
SYSTEM.OUT.PRINTLN ("Close connection ...");
try {
if (rs! = null) {
Rs.close ();
System.out.println ("closed resultset");
}
if (PS! = null) {
Ps.close ();
SYSTEM.OUT.PRINTLN ("closed statement");
}
IF (conn! = null) {
Conn.close ();
SYSTEM.OUT.PRINTLN ("closed connection");
}
} catch (Exception e) {
E.printstacktrace ();
}
}
/**
* @param args
* @throws illegalaccessexception
* @throws instantiationexception
*/
public static void Main (string[] args) {
Jdbcdemo demo = new Jdbcdemo ();
Demo.getconnection ();
String sql = "Delete from type where id = 1";
String Sql_1 = "INSERT into type values (1, ' teaching equipment ')";
String sql_2 = "SELECT * from Software_item limit 1;";
Demo.getstatement (SQL);
Demo.getstatement (Sql_1);
Demo.getstatement (sql_2);
ResultSet rs = Demo.getresultset ();
try {
while (Rs.next ()) {
System.out.println ("" + rs.getint (1) + ");
System.out.println (rs.getstring (2));
System.out.println (Rs.getint (3));
}
}catch (SQLException e) {
E.printstacktrace ();
}
Demo.closeconnection ();
}
}
Java Operations Database