Java connection mysql database!

xiaoxiao2021-03-06  22

Package com.roytel.rtccp.util;

Import java.sql. *;

PUBLIC CLASS DBMANAGER {

//username

PRIVATE STRING User = ""

//password

Private string password = ""

// host

Private string host = "";

// Database name

Private string database = ""

/ *

Private string url = "jdbc: mysql: //" host "/" "useunicode = true & characterencoding = GB2312";

* /

Private string url = "";

Private connection con = NULL;

StMT;

/ **

* Name the host, database name, database username, and database user passwords.

* @Param Host String

* @Param Database String

* @Param User String

* @Param Password String

* /

Public dbmanager (String Host, String Database, String User, String Password) {

THIS.HOST = Host;

THIS.DATABASE = DATABASE;

THIS.USER = User;

this.password = password;

// Show Chinese

THIS.URL = "JDBC: mysql: //" Host "/" Database

"? useunicode = true & characterencoding = GB2312";

Try {

Class.Forname ("org.gjt.mm.mysql.driver");

}

Catch (classnotfoundexception e) {

System.err.println ("Class Not Found:" E.GETMESSAGE ());

}

Try {

Con = DriverManager.getConnection (this.url, this.user, this.password);

/ / Connection type is resultSet.Type_Scroll_insensitive, ResultSet.concur_read_only

Stmt = con.createstatement (ResultSet.Type_Scroll_INSensitive,

ResultSet.concur_read_only);

}

Catch (SQLException a) {

System.err.Println ("SQL Exception:" A.getMessage ());

}

}

/ **

* Returns the connected connection

* /

Public connection getCon () {

Return con;

}

/ **

* Implement a simple query statement

* Returns the result set

* /

Public ResultSet ExecuteQuery (String SQL) {

ResultSet RS = NULL;

Try {

RS = stmt.executequery (SQL);

}

Catch (SQLException E) {

E.PrintStackTrace ();

}

Return RS;

}

/ **

* Implement a simple update statement

* Returns true *

* /

Public Boolean ExecuteUpdate (String SQL) {

Boolean v = false;

Try {

v = stmt.executeUpdate (SQL)> 0? true: false;

}

Catch (SQLException E) {

E.PrintStackTrace ();

}

Finally {

Return V;

}

}

}

It can be used, you can expand, for example, how to increase the method of executing the precompiled statement, the method of performing the stored procedure, or the method of connecting the pool.

转载请注明原文地址:https://www.9cbs.com/read-79339.html

New Post(0)