⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connecter.java

📁 这是基于jsp的相册管理系统。数据库是mysql
💻 JAVA
字号:
package huitong.util.db;

import java.sql.*;
public class Connecter
{
    private static String driverName =  "sun.jdbc.odbc.JdbcOdbcDriver";//ODBC
    //private static String driverName =  "com.mysql.jdbc.Driver";//MySQL
	//private static String driverName =  "mm.mysql-2.0.14.org.gjt.mm.mysql.Driver";//MySQL 官方
	
	 private static  String url = "jdbc:odbc:huitong";//odbc
	 //private static  String url = "jdbc:mysql://localhost:3306/huitong";
   //jdbc:mysql://localhost:3306/sample_db?user=root&password=your_password
   private static String user = "huitong";
   private static String pwd = "hongxu";
   private static Connection con;
   private Statement state;
   static 
   {
	   try
	   {
		   Class.forName(driverName);
		   //Class.forName("com.mysql.jdbc.Driver");

//		   Class.forName("com.mysql.jdbc.Driver").newInstance();

		   con = DriverManager.getConnection(url,"","");//ODBC
		  //con = DriverManager.getConnection(url,user,pwd);//MySQL
		   
	   }
	   catch (ClassNotFoundException e)
	   {
		   e.printStackTrace();
	   }
	   catch (SQLException e)
	   {
		   e.printStackTrace();
	   }
   }
   public  Connecter()
   {
	   
   }
   public Connecter(String driverName)
   {
	   if (driverName.equals(this.driverName))return ;
	   try
	   {
		   Class.forName(driverName);
	   }
	   catch (ClassNotFoundException e)
	   {
		   e.printStackTrace();
	   }
   }
   public void  setURL(String url)
   {
	   this.url = url;
   }
   public void setUser(String user)
   {
	   this.user = user;
   }
   public void setPwd(String pwd)
   {
	   this.pwd = pwd;
   }
   
   public Connection createConnection()
   {
	   try
	   {
		  return  DriverManager.getConnection(url,user,pwd);		   
	   }
	   catch(SQLException e)
	   {
		   e.printStackTrace();
	   }

	   return con;
   }
   public Connection createConnection(String url,String user,String pwd)
   {
	   return createConnection();
   }
   
   public Statement getStatement()
   {
	   if (con == null) return null;
	   try
	   {
		   return con.createStatement();   
	   }
	   catch(SQLException e)
	   {
		   
	   }
	   return null;
   }
   public ResultSet getResultSet(String querySQL)
   {
	   
	   try 
	   {
		   state =  con.createStatement();
		   return state.executeQuery(querySQL);
		   
	   }
	   catch (SQLException e)
	   {
		   try
		   {
			   state = createConnection().createStatement();
			   return state.executeQuery(querySQL);
		   }
		   catch (SQLException ex)
		   {
			   ex.printStackTrace();
		   }
		   
	   }
	   return null;
   }
   public boolean insert(String sql)
   {
	   try 
	   {
		   state =  con.createStatement();
		   //、、、、注意execute()
		   state.execute(sql);//如果结果多行记录,则返回true 
		   return true;
	   }
	   catch (SQLException e)
	   {
		   try
		   {
			    state = createConnection().createStatement();
			    state.execute(sql);
			    return true;
		   }
		   catch (SQLException ex)
		   {
			   ex.printStackTrace();
		   }
		   
	   }
	   return false;
   }
   public int update(String sql)
   {
	   
	   try 
	   {
		   state =  con.createStatement();
		   return state.executeUpdate(sql);
		   
	   }
	   catch (SQLException e)
	   {
		   e.printStackTrace();
		   try 
		   {
			   state =  con.createStatement();
			   return state.executeUpdate(sql);
			   
		   }
		   catch (SQLException ex)
		   {
			   ex.printStackTrace();
		      return 0;
		   }
	   }
   }
   public boolean delete(String sql)
   {
	   try 
	   {
		    state =  con.createStatement();
		    state.executeUpdate(sql);
		    return true;
	   }
	   catch (SQLException e)
	   {
		   try 
		   {
			    state =  con.createStatement();
			    state.executeUpdate(sql);
			    return true;
		   }
		   catch (SQLException ex)
		   {
			   ex.printStackTrace();
			   return false;
		   }
	   }
	  
   }
   
   public static  void main(String[] agrs)
   {
	  Connection con = new Connecter().createConnection();
	  System.out.println(con);
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -