常用 mysql 操作指令:
- 连接:
mysql -u用户名 -p密码
- 退出:
mysql>exit;
- 建数据库:
mysql>create database ???;
- 显示数据库:
mysql>show databases;//(-s)
- 删除数据库:
mysql>drop database ???;
- 连接数据库:
mysql>use ???;
- 创建数据表:
mysql>create table ???(字段名 类型,字段名 类型);
- 删除表:
mysql>drop table ???;
- 表插入数据:
mysql>insert into 表名 value(?,?,...);//与建表元素个数相同,类型相符
- 表查数据:
mysql>select ??? from 表名 where ??; mysql>select * from 表名;
- 表查前几行数据:
mysql>select * from 表名 order by id limit 0,2;
- 删除表中数据:
mysql>delete from 表名 where ???;
- 修改表中数据:
mysql>update 表名 set name="???" where id=1?
如若以上内容不够用,可以参考这里。
在实际的 JAVA WEB 中,就应当把数据库的驱动,连接,用户名和密码写在配置文件中。这样才便于维护。
以 Properties 读取配置文件:
java.properties: driver=com.mysql.jdbc,Driver url=jdbc:mysql://localhost:3306/test user=root password=5938
引入:
import java.io.InputStream; import java.util.Properties;
读取操作:
static{ Properties prop = new Properties(); InputStream in = 类名.class.getClassLoader().getResourceAsStream("jdbc.properties"); try{ prop.load(in); } catch(IOException e){ e.printStackTrace(); } driver=prop.getProperty("driver"); url=prop.getProperty("url"); user=prop.getProperty("user"); passwood=prop.getProperty("password"); }
完整的链接程序如下:
package ppj.demo01.jdbc; import java.io.InputStream; import java.util.Properties; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * test to connection sql * 单线程链接 * @author pengpj * */ public class JDBCConnectionTest01 { /** * 数据库连接设置 * driver 数据库驱动 * url 数据库链接字符 * user 用户名 * password 密码 */ public static String driver=null;//"com.mysql.jdbc.Driver"; public static String url = null;//"jdbc:mysql://localhost:3306/test"; public static String user = null;//"root"; public static String password = null;//"5938"; /** * 在加载类时,将jdbc链接数据库的信息获取 * 获取途径:jdbc.properties */ static { Properties prop = new Properties(); InputStream in = JDBCConnectionTest01.class.getClassLoader().getResourceAsStream("jdbc.properties"); try{ prop.load(in); }catch( IOException e){ e.printStackTrace(); } driver = prop.getProperty("driver"); url = prop.getProperty("url"); user = prop.getProperty("user"); password = prop.getProperty("password"); } /* * 加载驱动 */ { try{ Class.forName(driver); }catch(ClassNotFoundException e){ System.out.println("链接失败!\nnot found the exception"); e.printStackTrace(); } } /* * 获取链接 */ public static Connection getConne(){ Connection conn = null; try{ conn = DriverManager.getConnection(url, user, password); }catch(SQLException e){ e.printStackTrace(); } return conn; } /* * 关闭结果集 */ public static void close(ResultSet rs){ if(null!=rs){ try{ rs.close(); }catch(SQLException e){ e.printStackTrace(); } } } /* * 关闭封装SQL命令的对象 */ public static void close(Statement stmt){ if(null!=stmt){ try{ stmt.close(); }catch(SQLException e){ e.printStackTrace(); } } } /* * 关闭链接 */ public static void close(Connection conn){ if(null!=conn){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); } } } }
测试类如下:
package ppj.demo01; import ppj.demo01.jdbc.JDBCConnectionTest01; import static org.junit.Assert.*; import org.junit.Test; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; public class testJDBCConnectionTest01 { @Test public void testGetConne() throws Exception{ Connection conn = null; Statement stmt = null; ResultSet rs = null; String sqlstr = "select * from user"; try{ conn = JDBCConnectionTest01.getConne(); stmt = conn.createStatement(); rs = stmt.executeQuery(sqlstr); while(rs.next()){ int id = rs.getInt("id"); String userName = rs.getString("userName"); String addr = rs.getString("addr"); System.out.println("id:"+id+" userName:"+userName+" address:"+addr); } }catch(Exception e){ }finally{ JDBCConnectionTest01.close(conn); JDBCConnectionTest01.close(rs); JDBCConnectionTest01.close(stmt); } assertNotEquals("false",JDBCConnectionTest01.getConne(),JDBCConnectionTest01.getConne()); } }
注:
在数据库中已经建立有库 test 与表 user,所以可以在程序中直接链接调用而不用再建表。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于