ÓÃjavaÁ¬½ÓMySqlÊý¾Ý¿â
Ò»¸ö¼òµ¥Ê¾Àý--
1£¬×¼±¸£ºMySQLÊý¾Ý¿âÇý¶¯°ü¡¾mysql-connector-java-5.1.10-bin.jar¡¿µ¼Èë
2£¬´´½¨²âÊÔÁ¬½ÓÖ÷³ÌÐò
package mysqlConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class JdbcDemo {
public static void main(String[] args) {
try {
//Êý¾Ý¿âÁ¬½ÓMySQL
Connection conn=null;
Statement stmt=null;
//MySQLÇý¶¯Ãû
String className="com.mysql.jdbc.Driver";
//ÒªÁ¬½ÓµÄURL,localhostÊÇÁ¬½Ó±¾µØ£¬Èç¹ûÁ¬½ÓÆäËû»úÆ÷¸ÄΪIPµØÖ·3306ÊÇMysqlĬÈ϶˿ڣ¬testÊÇÄãÒª²Ù×÷µÄÊý¾Ý¿âµÄÃû³Æ£¬Êý¾Ý¿âÒªÓвÅÐÐ
String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8";
//mySqlµÄÓû§ÃûÃÜÂëÒª¸ÄΪÄã×Ô¼ºµÄmysqlÊý¾Ý¿âµÄÓû§Ãû¡¢ÃÜÂë
String user="root";
String password="1314520";
//ÏÂÃæÈýÐÐÊǹ̶¨Ð´·¨
Class.forName(className);
conn=DriverManager.getConnection(url, user, password);
stmt=conn.createStatement();
//¿ªÊ¼²åÈë²Ù×÷£¬ut_testÊÇtestÊý¾Ý¿âÖеÄÒ»ÕÅ±í£¬ÓÐname,age,emailÈý¸ö×ֶΣ¬²åÈëµÄÖµÊÇ'ÁõÒü',22,'354559242@qq.com'¡£
String sql="insert into ut_test(name,age,email) values ('ÁõÒü',22,'354559242@qq.com') ";
//Ö´ÐвÙ×÷
stmt.execute(sql);
System.out.println("²åÈë³É¹¦£¡");
} catch(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Ïà¹ØÎĵµ£º
Java NIO APIÏê½â
ÔÚJDK
1.4ÒÔÇ°£¬JavaµÄIO²Ù×÷¼¯ÖÐÔÚjava.ioÕâ¸ö°üÖУ¬ÊÇ»ùÓÚÁ÷µÄ×èÈû£¨blocking£©API¡£¶ÔÓÚ´ó¶àÊýÓ¦ÓÃÀ´Ëµ£¬ÕâÑùµÄAPIʹÓúܷ½
±ã£¬È»¶ø£¬Ò»Ð©¶ÔÐÔÄÜÒªÇó½Ï¸ßµÄÓ¦Óã¬ÓÈÆäÊÇ·þÎñ¶ËÓ¦Óã¬ÍùÍùÐèÒªÒ»¸ö¸üΪÓÐЧµÄ·½Ê½À´´¦ÀíIO¡£´ÓJDK 1.4Æð£¬NIO
API×÷Ϊһ¸ö»ùÓÚ»º³åÇø£¬²¢ÄÜÌṩ·Ç×èÈû(non-blo ......
package com.xinnuo.jdbc;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;
/*
* ¸ÃÀàÖ»ÄÜ´´½¨Ò»¸öʵÀý£¬ÆäËü¶ÔÏóÄܹ»µ÷ÓÃÆ侲̬·½·¨£¨Ò²³ÆΪÀà·½·¨£©»ñµÃ¸ÃΨһʵÀýµÄÒýÓá£
* DBConnectionManagerÀàµÄ½¨¹¹º¯ÊýÊÇ˽Óеģ¬ÕâÊÇΪÁ˱ÜÃâÆäËü¶ÔÏó´´½¨¸ÃÀ ......
ÏëÏë×Ô¼ºÃ÷Äê¾ÍÒª±ÏÒµÁË£¬µ«×Ô¼ºÐÄÀï×ÜÊǺܵ£ÐÄ£¬Ò²²»ÖªµÀ×Ô¼ºÑ§Õâ¸öµ½µ×ÊǶԻ¹ÊÇ´í£¡×ÜÊÇÌý˵ÏÖÔÚ×ö³ÌÐòµÄ¹¤×÷²»ºÃÕÒ£¬¹¤×ʺܵͣ¬ÏÖÔÚÓкöàͬѧ¶¼Ã»Ñ§ÁË£¬Ò²²»ÖªµÀ×Ô¼ºÊDz»ÊÇ»¹Òª¼á³ÖÏÂÈ¥£¬ÊDz»ÊÇ»¹ÒªjiavaÕâÌõ·¡£¸Ð¾õºÃÃÔã....... ......
¡¡finalÀࣺΪ·ÀÖ¹ËûÈË´ÓÄãµÄÀàÉÏÅÉÉúÐÂÀ࣬´ËÀàÊDz»¿ÉÀ©Õ¹µÄ¡£
¡¡¡¡¶¯Ì¬µ÷ÓñȾ²Ì¬µ÷Óû¨·ÑµÄʱ¼äÒª³¤¡£
¡¡¡¡³éÏóÀࣺ¹æ¶¨Ò»¸ö»ò¶à¸ö³éÏó·½·¨µÄÀà±¾Éí±ØÐ붨ÒåΪabstract¡£
¡¡¡¡Àý£º public abstract string getDescripition
¡¡¡¡JavaÖеÄÿһ¸öÀ඼ÊÇ´ÓObjectÀàÀ©Õ¹¶øÀ´µÄ¡£
¡¡¡¡objectÀàÖеÄequalºÍtoString·½·¨¡ ......
MySQLÖÐÓкܶàµÄ»ù±¾ÃüÁshowÃüÁîÒ²ÊÇÆäÖÐÖ®Ò»£¬ÔںܶàʹÓÃÕßÖжÔshowÃüÁîµÄʹÓû¹ÈÝÒײúÉú»ìÏý£¬±¾ÎĻ㼯ÁËshowÃüÁîµÄÖÚ¶àÓ÷¨¡£
a. show tables»òshow tables from database_name; -- ÏÔʾµ±Ç°Êý¾Ý¿âÖÐËùÓбíµÄÃû³Æ¡£
b. show databases; -- ÏÔʾmysqlÖÐËùÓÐÊý¾Ý¿âµÄÃû³Æ¡£
c. show columns from table_nam ......