Java Operations Database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.