1. 程式人生 > >Java通過jdbc遠端操控MySQL資料庫

Java通過jdbc遠端操控MySQL資料庫

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Mydatabase 
{
    static Statement statement;
    static Connection connection;

     public Mydatabase()
    {


    }
     //用於連線資料庫
     private
static void connect() { try { try { Class.forName( "com.mysql.jdbc.Driver");//載入資料庫驅動 System.out.println("資料庫驅動載入成功"); }catch(ClassNotFoundException e){} //連線資料庫 connection=DriverManager.getConnection("jdbc:mysql://125.250.223.51:3306/words?characterEncoding=utf8"
,"root",""); //其中125.250.223.51要換成資料庫端所在的IP地址,3306改為資料庫的埠號 statement=connection.createStatement(); }catch(Exception e){ e.printStackTrace(); System.out.println("sorry,連線失敗");} } //向資料庫word表中插入詞語 public void insert
(String s) throws SQLException { connect(); try { statement.executeUpdate("INSERT INTO word VALUES ('"+s+"','"+s.length()+"')"); } catch (SQLException e) { System.out.println("已經有這個詞語"); e.printStackTrace(); } try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //刪除資料庫word表的某個詞語 public void delete(String s ) { connect(); try { statement.executeUpdate("DELETE FROM word WHERE description='"+s+"'"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //隨機從資料庫word表中獲得一個詞語 public String getWord() { connect(); ResultSet set=null; try { set=statement.executeQuery("SELECT*FROM word ORDER BY rand() LIMIT 5"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { while(set.next()) { return set.getString(1); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "not"; } //賬戶的註冊 public void register(String user ,String password) { connect(); try { statement.executeUpdate("INSERT INTO account VALUES ('"+user+"','"+password+"')"); } catch (SQLException e) { System.out.println("使用者已存在"); e.printStackTrace(); } try { statement.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { connection.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //使用者的登入 public boolean login(String user ,String password) { connect(); ResultSet set=null; try { set=statement.executeQuery("SELECT password FROM account where user='"+user+"'" ); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { while(set.next()) { if(password.equals(set.getString(1))) return true; else return false; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } }