Guest User

Untitled

a guest
May 30th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.24 KB | None | 0 0
  1. package wiki.home;
  2. import java.sql.*;
  3.  
  4. public class jdbcmysql {
  5.       private Connection con = null; //Database objects
  6.     //連接object
  7.       private Statement stat = null;
  8.       //執行,傳入之sql為完整字串
  9.       private ResultSet rs = null;
  10.       //結果集
  11.       private PreparedStatement pst = null;
  12.       //執行,傳入之sql為預儲之字申,需要傳入變數之位置
  13.       //先利用?來做標示
  14.      
  15.       private String dropdbSQL = "DROP TABLE NE_TABLE ";
  16.      
  17.       private String createdbSQL = "CREATE TABLE NE_TABLE (" +
  18.         "    id     INTEGER " +
  19.         "  , length  INTEGER " +
  20.         "  , ne  VARCHAR(200) " +
  21.         "  , category    VARCHAR(200) " +
  22.         "  , rdd  longtext)";
  23.      
  24.       private String insertdbSQL = "insert into NE_TABLE(id,length,ne,category,rdd) " +
  25.           "select ifNULL(max(id),0)+1,?,?,?,? FROM NE_TABLE";
  26.      
  27.       private String selectSQL = "select * from NE_TABLE ";
  28.      
  29.       public void resetSelectSQL(String SQL)
  30.       {
  31.           this.selectSQL = SQL;
  32.       }
  33.       public jdbcmysql()
  34.       {
  35.         try {
  36.           Class.forName("com.mysql.jdbc.Driver");
  37.           //註冊driver
  38.           con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Jaxon","root","iisr@1706b");
  39.           //取得connection
  40.      
  41.           //jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=Big5
  42.           //localhost是主機名,test是database名
  43.           //useUnicode=true&characterEncoding=Big5使用的編碼
  44.          
  45.         }
  46.         catch(ClassNotFoundException e)
  47.         {
  48.           System.out.println("DriverClassNotFound :"+e.toString());
  49.         }//有可能會產生sqlexception
  50.         catch(SQLException x) {
  51.           System.out.println("Exception :"+x.toString());
  52.         }
  53.      
  54.       }
  55.       //建立table的方式
  56.       //可以看看Statement的使用方式
  57.       public void createTable()
  58.       {
  59.         try
  60.         {
  61.           stat = con.createStatement();
  62.           stat.executeUpdate(createdbSQL);
  63.         }
  64.         catch(SQLException e)
  65.         {
  66.           System.out.println("CreateDB Exception :" + e.toString());
  67.         }
  68.         finally
  69.         {
  70.           Close();
  71.         }
  72.       }
  73.       //新增資料
  74.       //可以看看PrepareStatement的使用方式
  75.       public void insertTable( int length,String ne,String category,String rdd)
  76.       {
  77.         try
  78.         {
  79.           pst = con.prepareStatement(insertdbSQL);
  80.      
  81.           pst.setInt(1, length);
  82.           pst.setString(2, ne);
  83.           pst.setString(3, category);        
  84.           pst.setString(4, rdd);
  85.           pst.executeUpdate();
  86.         }
  87.         catch(SQLException e)
  88.         {
  89.           System.out.println("InsertDB Exception :" + e.toString());
  90.         }
  91.         finally
  92.         {
  93.           Close();
  94.         }
  95.       }
  96.       //刪除Table,
  97.       //跟建立table很像
  98.       public void dropTable()
  99.       {
  100.         try
  101.         {
  102.           stat = con.createStatement();
  103.           stat.executeUpdate(dropdbSQL);
  104.         }
  105.         catch(SQLException e)
  106.         {
  107.           System.out.println("DropDB Exception :" + e.toString());
  108.         }
  109.         finally
  110.         {
  111.           Close();
  112.         }
  113.       }
  114.       //查詢資料
  115.       //可以看看回傳結果集及取得資料方式
  116.       public void SelectTable()
  117.       {
  118.         try
  119.         {
  120.           stat = con.createStatement();
  121.           rs = stat.executeQuery(selectSQL);
  122.           System.out.println("ID\t\tLENGTH\t\tNE\t\tCategory\tRDD");
  123.           while(rs.next())
  124.           {
  125.             System.out.println(rs.getInt("id")+"\t\t"+ rs.getInt("length")+"\t\t"+
  126.                     rs.getString("ne")+"\t\t"+rs.getString("category")+"\t\t"+rs.getString("rdd"));
  127.           }
  128.         }
  129.         catch(SQLException e)
  130.         {
  131.           System.out.println("DropDB Exception :" + e.toString());
  132.         }
  133.         finally
  134.         {
  135.           Close();
  136.         }
  137.       }
  138.       //完整使用完資料庫後,記得要關閉所有Object
  139.       //否則在等待Timeout時,可能會有Connection poor的狀況
  140.       private void Close()
  141.       {
  142.         try
  143.         {
  144.           if(rs!=null)
  145.           {
  146.             rs.close();
  147.             rs = null;
  148.           }
  149.           if(stat!=null)
  150.           {
  151.             stat.close();
  152.             stat = null;
  153.           }
  154.           if(pst!=null)
  155.           {
  156.             pst.close();
  157.             pst = null;
  158.           }
  159.         }
  160.         catch(SQLException e)
  161.         {
  162.           System.out.println("Close Exception :" + e.toString());
  163.         }
  164.       }
  165. }
Add Comment
Please, Sign In to add comment