朕已無力吐糟實驗室的網速了。。。html
10點半左右開機,網線、wifi都連不上,右下角wifi連上但有黃色歎號,java
QQ錯誤碼:0x00000001 表示訪問網絡失敗mysql
ipconfig,擦,本機爲私有IP,連路由器都連不上,搞毛線啊。git
使出必殺技:疑難解答,顯示無線訪問點問題,但其餘人又能連上,無語;sql
思科路由器太不穩定了吧,使出終極必殺技,保持黃色歎號的狀態,吃飯睡覺2小時左右,回來發現黃色歎號消失,——這絕壁是路由器問題吧?數據庫
連上以後上Q,數組
QQ錯誤碼:0x00000050 表示DNS錯誤服務器
QQ電腦管家的DNS選優工具測試一下,反正都那麼慢,還原到初試DNS好了;網絡
QQ能夠登陸了,世界再次恢復和平。函數
mysql 服務器支持 # 到該行結束、-- 到該行結束 以及 /* 行中間或多個行 */ 的註釋方格: mysql> SELECT 1+1; # 這個註釋直到該行結束 mysql> SELECT 1+1; -- 這個註釋直到該行結束 mysql> SELECT 1 /* 這是一個在行中間的註釋 */ + 1; mysql> SELECT 1+ /* 這是一個 多行註釋的形式 */ 1; 儘管服務器理解剛纔描述的註釋句法,但 MySQL 客戶端的語法分析在 /* ... */ 註釋方式上還有所限制:
navicat中配置字體爲consoles,F6打開命令行界面,簡直是Matlab的感腳啊。
mysql> select * from student;
+----+------+--------+
| ID | name | class |
+----+------+--------+
| 1 | tom | 計算機 |
| 0 | NULL | NULL |
| 0 | NULL | NULL |
+----+------+--------+
3 rows in setmysql> show tables;
+------------------+
| Tables_in_db0703 |
+------------------+
| student |
+------------------+
1 row in setmysql> show table;
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
mysql> show database;
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1
mysql> create database demo;
Query OK, 1 row affectedmysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db0703 |
| demo |
| mysql |
| test |
+--------------------+
5 rows in setmysql> select * from student;
+----+------+--------+
| ID | name | class |
+----+------+--------+
| 1 | tom | 計算機 |
| 0 | NULL | NULL |
| 0 | NULL | NULL |
+----+------+--------+
3 rows in setmysql> use demo;
Database changed
mysql> show tables;
Empty setmysql> create table demoTable(
-> ID int not null,
-> user varchar(16) not null
-> );
Query OK, 0 rows affected
mysql> show tables;
+----------------+
| Tables_in_demo |
+----------------+
| demotable |
+----------------+
1 row in setmysql> --刪除表
-> --drop table demoTable;
-> --顯示錶的結構
-> describe demoTable;
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '--刪除表--drop table demoTable;
--顯示錶的結構 #【此處「--」後面加個空格就行了】
describe de' at line 1
mysql> describe demoTable;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ID | int(11) | NO | | NULL | |
| user | varchar(16) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in setmysql> insert into demotable values(1,'admin');
Query OK, 1 row affectedmysql> select * from demotable;
+----+-------+
| ID | user |
+----+-------+
| 1 | admin |
+----+-------+
1 row in setmysql> delete from demotable where id=2;
Query OK, 0 rows affectedmysql>
簡單入門就先show databases;再use DBname;
本例中引入mysql-connector-java-5.1.22.zip中jar包(參考http://www.cnblogs.com/fickleness/archive/2013/06/27/3158687.html)
運行結果
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestMysql { public static void main(String[] args){ // 驅動程序名 String driver = "com.mysql.jdbc.Driver"; // URL指向要訪問的數據庫名db0703 String url = "jdbc:mysql://localhost:3306/db0703"; // MySQL配置時的用戶名 String user = "root"; // MySQL配置時的密碼 String password = ""; try { // 加載驅動程序 Class.forName(driver); // 連續數據庫 Connection conn = DriverManager.getConnection(url, user, password); if(!conn.isClosed()) System.out.println("Succeeded connecting to the Database!"); // statement用來執行SQL語句 Statement statement = conn.createStatement(); // 要執行的SQL語句 String sql = "select * from student"; // 結果存儲到rs中,可能多條數據,須要循環讀取 ResultSet rs = statement.executeQuery(sql); System.out.println("-----------------"); System.out.println("執行結果以下所示:"); System.out.println("-----------------"); System.out.println(" id" + "\t" + "name"+"\t"+"class"); System.out.println("-----------------"); //初始化變量 int ID=-1; String name = null; String classname= null; //循環讀取 while(rs.next()) { // 讀取3列數據 ID = rs.getInt("ID"); name=rs.getString("name"); classname=rs.getString("class"); //輸出結果 System.out.println(ID + "\t" + name+"\t"+classname); // 首先使用ISO-8859-1字符集將name解碼爲字節序列並將結果存儲新的字節數組中。 // 而後使用GB2312字符集解碼指定的字節數組 //name = new String(name.getBytes("ISO-8859-1"),"GB2312"); } //依次關閉ResultSet對象、Statement對象、Connection對象,最好用if判斷一下是否爲空 rs.close(); statement.close(); conn.close(); } catch(ClassNotFoundException e) { System.out.println("Sorry,can't find the Driver!"); e.printStackTrace(); } catch(SQLException e) { e.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } } }
http://yanqiaosun.blog.163.com/blog/static/216133133201302795115878/
1、java鏈接MySQL數據庫
一、安裝mysql connector/j驅動
添加jdbc驅動: mysql-connection-java-5.1.10-bin.jar
二、鏈接MySQL數據庫
在java.sql包中存在DriverManager類、Connection接口、Statement接口和ResultSet接口。
DriverManager類:管理驅動程序和鏈接數據庫
Connection接口:管理創建好的數據庫鏈接
Statement接口:執行sql語句
ResultSet接口:存儲數據庫返回的記錄
forName()方法指定MySQL驅動的語法以下:
class.forname("com.mysql.jdbc.Driver");
class.forName("org.git.mysql.Driver");
兩個驅動其實是同樣的,後者繼承前者。
getConnection()方法有三個參數,分別是url、user和password。
url指定idbc的數據源,數據源必需要指定他的IP、端口號等。
Connection connection=DriverManager.getConnection(url,user,password); //建立connection對象
例子:
鏈接本地計算機的mysql數據庫,mysql使用默認端口3306,鏈接的數據庫是syq,使用用戶root來鏈接,root用戶的密碼是123456.鏈接mysql
的語句以下:
Connection
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/syq","root","123456");
經過這個語句就能夠鏈接到MySQL的syq數據庫了。該語句也能夠寫成下面的形式:
String url="jdbc:mysql://localhost:3306/syq"; //獲取協議、IP、端口等信息
String user="root"; //獲取數據庫用戶名
String password="123456";//獲取數據庫用戶密碼
Connection connection=DriverManager.getConnection(url,user,password); //建立Connection對象
2、java操縱MySQL數據庫
鏈接MySQL數據庫以後,能夠對MySQL數據庫中的數據進行查詢、插入、更新、刪除等操做。Statement接口主要用來執行sql語句,其中定義一些執行sql語句的方法。sql語句執行後返回結果由ResultSet接口管理。經過這兩個接口,java能夠方便的操做MySQL數據庫。
一、建立Statement對象
Connection對象調用createStatement()方法來建立Statement對象,其代碼以下:
Statement statement=connection.createStatement();
其中,statement是Statement對象;createStatement()方法返回Statement對象。經過這個java語句就能夠建立Statement對象。Statement對
象建立成功後,能夠調用其中的方法來執行SQL語句。
二、使用select語句查詢數據
Statement對象能夠調用excuteQuery()方法執行select語句。select的查詢結果返回給ResultSet對象。調用excuteQuery()方法的代碼是:
ResultSet result=statement.excuteQuery("select語句");
經過該條語句能夠將查詢結果存儲 到result中。查詢結果可能有多條記錄,這就須要使用循環語句來讀取全部記錄,其代碼以下:
while(result.next()){
String s=resule.getString("字段名");
System.out.print(s);
}
例子:
從score表中查詢學生的學號、考試科目和成績,部分代碼:
Statement statement=connection.createStatement();
Result result=statement.excuteQuery("select stu_id,c_name,grade from score");
while(result.next()){
String id=result.getString("stu_id");
String course=result.getString("c_name");
String grade=result.getString("grade");
System.out.println(id+" "+course+" "+grade);
}
三、插入更新或者刪除數據
excuteQuery()方法只能執行select語句。若是須要進行插入、更新或者刪除操做,則須要Statement對象調用excuteUpdate()方法來實現。
excuteUpdate執行完後,返回影響表的行數。下面是調用excuteUpdate()方法的代碼:
int result=statement.excuteUpdate(sql);
其中「sql」必須是insert語句、update語句或者delete語句。該方法返回的結果爲數字。
例子:
下面向score表插入一條新記錄,部分代碼:
Statement statement=connection.createStatement();
String sql="insert into score values(21,902,'英語',85)";
int result=statement.excuteUpdate(sql);
System.out.println(result);
更新score表中id爲16的記錄,將該記錄的grade字段值 改成100,部分代碼:
Statement statement=connection.createStatement();
String sql="UPDATE score set grade=100 where id=16";
int result=statement.excuteUpdate(sql);
System.out.println(result);
刪除score表中id爲16的記錄,部分代碼:
Statement statement=connection.createStatement();
String sql="delete from score where id=16";
int result=statement.excuteUpdate(sql);
System.out.println(result);
四、執行任意sql語句
沒法肯定要執行的語句是查詢仍是更新時,可使用excute()函數。該函數的返回結果是boolean類型的值,返回值爲true表示執行查詢語句,false表示執行更新語句。下面是調用excute()方法的代碼:
boolean result=statement.excute(sql);
五、關閉建立的對象
當全部sql語句都執行完畢後,須要關閉建立的Connection對象、Statement對象和Result對象。關閉對象的順序與建立對象的順序相反,關閉的順序爲ResultSet對象、Statement對象、Connection對象。對象調用close()方法來關閉對象,而後將對象的值設爲空。
關閉對象部分代碼:
if(result!=null){ //判斷ResultSet對象是否爲空
result.close();//調用close()方法關閉ResultSet對象
result=null;
}
if(statement!=null){
statement.close();
statement=null;
}
if(connection!=null){
connection.close();
connection=null;
}
3、java備份與還原MySQL數據庫
java語言中能夠執行mysqladmin命令來備份MySQL數據量,也能夠執行mysql命令來還原mysql數據庫。
一、java備份MySQL數據庫
一般使用mysqldump命令來備份MySQL數據庫,其語句以下:
mysqldump -u username -pPassword dbname table1 table2 …>BackupName.sql
調用exec()方法代碼:
Runtime rt=Runtime.getRuntime();
rt.exec("命令語句");
例子:Windows操做系統下java備份MySQL數據庫:
String str="mysqldump -u root -p123456 --opt test>C:/test.sql";//將mysqldump命令的語句賦值給str
Runtime rt=Runtime.getRuntime();//建立Runtime對象
rt.exec("cmd/c"+str);//調用exec()函數
二、java還原MySQL數據庫
一般使用mysql命令來還原MySQL數據庫,其語句以下:
mysql -u root -p [dbname]<backup.sql
Java還原MySql數據庫:
String str="mysql -u root -p123456 test<C:/test.sql";//將test.sql文件還原到名爲test的數據庫中
Runtime rt=Runtime.getRuntime();
rt.exec("cmd/c"+str);
所有內容