A practical JavaBean.

Source: Internet
Author: User
Tags getmessage stmt

Import java.sql.*;
Import java.util.*;
Import java.text.*;
Import java.io.*;
public class Jdbc_sql
{
String conn_str= "jdbc:mysql://localhost/heaven_news";//---------Database
String username= "root";//---------------Database user name
String password= "";//-------------Database Password
Private Connection Conn=null;
Private Statement Stmt=null;
ResultSet Rs=null;
----------------------Database Links----------------
Public Jdbc_sql ()
{
try{
Class.forName ("Org.gjt.mm.mysql.Driver");
}
catch (Java.lang.ClassNotFoundException e)
{
System.err.println ("Dbclass ():" +e.getmessage ());
}
try{
Conn=drivermanager.getconnection (Conn_str,username,password);
}
catch (SQLException ex) {
System.err.println ("Dbclass ():" +ex.getmessage ());
}
}
---------------------database Query-----------------
Public ResultSet query (String SQL)
{
try{
Stmt=conn.createstatement ();
Rs=stmt.executequery (SQL);
catch (SQLException ex) {
SYSTEM.ERR.PRINTLN ("Query ():" +ex.getmessage ());
}
Return RS;
}
-----------Database Modification---------
public void update (String sql)
{
try{
Stmt=conn.createstatement ();
Stmt.executeupdate (SQL);
catch (SQLException ex) {
System.err.println ("Update ():" +ex.getmessage ());
}
}
----------Close Data Source--
public void closestmt ()
{
Try{stmt.close ();
}
catch (SQLException ex) {
System.err.println ("closestmt ():" +ex.getmessage ());
}
}
----------Close Link---------
public void Closeconn ()
{Try{conn.close ();}
catch (SQLException ex) {System.err.println ("Closeconn ()" +ex.getmessage ());}
}
---------------------Convert Chinese
public string Tochinese (string s) {
if (s==null)
return null;
try{
String Convert=new string (s.getbytes ("gb2312"), "iso8859-1");
return convert;
}catch (Exception e) {}
return null;
}
--------------------Paging
Public String page1 (int num,int line,int page_line,int start,int page,string search,string filename) {
int pagetotal=line*page_line;//The total number of bars to display
int pagestart;//
int pagenum;//
int total= (num-start+line-1)/line;//total number of pages obtained
int total_start= (NUM+PAGETOTAL-1)/pagetotal;//get total offset
String returnstr= "";
if (start!=0)
{
int old_start=start-pagetotal;
returnstr+= "<a href=" +filename+ "page=0&start=" +old_start+search+ "><<</a>";
}
pagestart=start/pagetotal*page_line;//Get Current Page
for (int i=0;i<total&&i<page_line;i++)
{
pagenum=pagestart+i+1;
returnstr+= "<a href= +filename+" page= "+i+" &start= "+start+search+" > "+pagenum+" </a>;
}
if (total_start!= (start/pagetotal+1) &&num!=0)
{
int new_start=start+pagetotal;
returnstr+= "<a href=" +filename+ "page=0&start=" +new_start+search+ ">>></a>";
}
return returnstr;
}
-------------------------------Get Time
Public String GetPath () {
String datestring= "123";
try{
Java.text.SimpleDateFormat formatter=new Java.text.SimpleDateFormat ("yyyy-mm");
Java.util.Date currenttime_1=new java.util.Date ();
Datestring=formatter.format (currenttime_1);
}catch (Exception e) {
}
return datestring;
}
-------------------------------Get Time
Public String Strnowtime () {
String datestring= "123";
try{
Java.text.SimpleDateFormat formatter=new Java.text.SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
Java.util.Date currenttime_1=new java.util.Date ();
Datestring=formatter.format (currenttime_1);
}catch (Exception e) {
}
return datestring;
}
--------------------------Remove HTML---------------------------
public string HTMLEncode (string Str) {
Str=ebbreplace (Str, "<", "<");
Str=ebbreplace (STR, ">", ">");
Str=ebbreplace (Str, "\ n", "<BR>");
return Str;
}
--------------------------substitution Variable---------------------------
public string Ebbreplace (string str,string oldstr,string newstr) {
String returnstr= "";
int i,j,t,m,n;
n=0;
J=oldstr.length ();
if (Str.indexof (OLDSTR) >-1)
{
while (Str.indexof (oldstr,n) >-1)
{
I=str.length ();
if (Str.indexof (OLDSTR) ==0)
Str=newstr+str.substring (J,i);
Else
{
T=str.indexof (OLDSTR);
M= (T+J);
Str=str.substring (0,t) +newstr+str.substring (m,i);
N=t+newstr.length ()-j+1;
}
}
}
RETURNSTR=STR;
return returnstr;
}
----------------------------UBB Replace--------------------------
public string Lcreplace (string str,string bstr,string estr,string restr) {
String returnstr= "", str1= "", str2= "";
int i,j,n;
n=0;
if ((Str.indexof (BSTR) >-1) && ((Str.indexof (ESTR) >-1))
{
while (Str.indexof (bstr,n) >-1)
{
I=str.indexof (BSTR);
J=str.indexof (ESTR);
Str1=str.substring ((I+bstr.length ()), j);
Str2=ebbreplace (Restr, "$lichao $", Str1);
STR1=BSTR+STR1+ESTR;
Str=ebbreplace (STR,STR1,STR2);
N=i+str2.length ()-str1.length ();
}
}
RETURNSTR=STR;
return returnstr;
}
}

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.