📄 link_db.java
字号:
package project3;
import java.sql.*;
public class Link_db{
private static Connection con=null;
private Statement state=null;
private ResultSet rs=null;
private String Color[]={"yellow","deepink","darkcyan","deepskyblue","orange","lightslategray"};
/***************************************************
*函数名称:init()
*函数功能:创建数据库Statement
*返回值: void
*参数说明:无
*最后修改:白伟明
* 2004年8月2日
****************************************************/
public void init(Connection con){
try{
state=this.con.createStatement();
}catch(Exception e){
System.out.println("init");
System.out.println(e.getMessage());
}
}
/***************************************************
*函数名称:close()
*函数功能:关闭数据库连接
*返回值: void
*参数说明:无
*最或修改:白伟明
* 2004年7月28日
****************************************************/
public void close(){
try{
if (rs !=null) rs.close();
}catch(Exception e){
System.out.println("rs");
System.out.println(e.getMessage());
}
try{
if(state!=null) state.close();
}catch(Exception e){
System.out.println("state");
System.out.println(e.getMessage());
}
try{
if(con!=null) con.close();
}catch(Exception e){
System.out.println("close");
System.out.println(e.getMessage());
}
}
/***************************************************
*函数名称:select()
*函数功能:查询数据
*返回值: int
* k=-10;没有任何造作
* k=0; 没有符合条件数据
* k=-1;出现异常
* k>0;操作成功
*参数说明:无
*最后修改:白伟明
* 2004年8月2日
****************************************************/
public int select(String sql){
int k=-10;
this.init(this.getCon());
try{
k=0;
rs=state.executeQuery(sql);
if(rs.next()){
k=k+1;
}
}catch(Exception e){
k=-1;
System.out.println("select");
System.out.println(e.getMessage());
this.close();
}
this.close();
return k;
}
/***************************************************
*函数名称:update()
*函数功能:更新、删除、插入数据
*返回值: int
* k=-10;没有任何造作
* k=0; 没有插入数据
* k=-1;出现异常
* k>0; 操作成功
*参数说明:无
*最或修改:白伟明
* 2004年8月2日
****************************************************/
public int update(String sql){
int k=-10;
this.init(this.getCon());
try{
k=0;
k=state.executeUpdate(sql);
}catch(Exception e){
k=-1;
System.out.println("update");
System.out.println(e.getMessage());
}
this.close();
return k;
}
/***************************************************
*函数名称:getCon()
*函数功能:创建数据库连接
*返回值: Connection
* con; 返回数据库连接
*参数说明:无
*最后修改:白伟明
* 2004年8月2日
****************************************************/
public static synchronized Connection getCon(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:prodb","","");
}catch(Exception e){
System.out.println("getCon");
System.out.println(e.getMessage());
}
return con;
}
/***************************************************
*函数名称:chStr()
*函数功能:做编码转换
*返回值: String
* 返回中文字符串
*参数说明:str
*最后修改:白伟明
* 2004年8月2日
****************************************************/
public String chStr(String str){
if(str==null){
str="";
}else{
try{
str=(new String(str.getBytes("iso-8859-1"),"GB2312")).trim();
}catch(Exception e){
System.out.println("chStr");
System.out.println(e.getMessage());
}
}
return str;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -