SQLServer 数据库使用JDBC连接步骤
//加载驱动类
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); //注册驱动, 执行Class.forName()的时候系统会自动执行加载类的static块,其中的代码就是向DriverManager注册sqlServer的驱动
//建立连接通道
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=你要连得数据库的名字" //创建连接
String user="sa";
String password=" ";
Connection conn=DriverManager.getConnection(url,user,password);//连接数据库
//执行sql语句
Statement stmt=conn.createStatement();//创建一个Statement对象来将sql语句发送到数据库
String sql=" "
int number=stmt.executeUpdate(sql)// 执行sql语句
//关闭连接
conn.close();
MYSQL数据库使用JDBC连接
//注册驱动
Class.forName("org.gjt.mm.mysql.Driver").new Instance();
//创立连接
String url=""jdbc:mysql://localhost/你要连接的数据库";
String user=" ";
String password=" ";
Connection conn=DriverManager.getConnection(url);//连接数据库
//执行sql语句
Statement stmt=conn.createStatement();//创建一个statement对象用于吧sql语句返回给数据库
String sql=" ";
int num=stmt.executeUpdate(sql)//执行sql语句
//关闭连接
conn.close();
一些数据库查询语句
一、几个简单的基本的sql语句
user 表名
选择:select * from user where id=3;
插入:insert into user( id,name) values( 4," 李四")
删除:delete from user where id=2;
更新:update user set name=" lisi" where name="李四"
查找:select * from user where name like ' %李%'---like 模糊查询
排序: select * from user order by id[desc]
总数:select count as totalcount from user
求和: select sum(field1)as sumvalue from user
平均:select avg (field1) as avgvalue from user
最大:select max(field1)as maxvalue from user
最小:select min(field1)as minvalue from user
几个高级查询运算符
A:union
union运算符通过组合其他两个结果表(例如table1和table2)并消除表中任何重复行而派生出一个结果表。当all随union一起使用时(即union all),不消除重复行,两种情况下,派生表的每一行不是来自table1就是来自table2
B:except运算符
except运算符通过包括所有在table1中但不在table2中的行并消除所有重复行而派生出一个结果表。当all 随except一起使用时(except all),不消除重复行
C:intersect运算符
intersect运算符通过只包括table1和table2中都有的行并消除所有重复行而派生出一个结果表。当all 随intersect一起使用时(intersect all),不消除重复行
注:使用运算词的几个查询结果行必须是一致的.
3 使用外连接
A : left out join:
左外连接:结果集包括连接表的匹配行,也包括左连接表的所有行
sql:select a.a,a.b,a.c,b.c,b.d,b.f from a left out join b on a.a=b.c
B:right out join 右外连接
结果集既包括连接表的匹配行也包括左连接表的所有行
C:full out join
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表的所有记录