import java.sql.*;
public class TTTest{
public static void main(String[]args){
Connection ct=null;
Statement sm=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//相当于加载了一个驱动程序(若想让计算机访问硬件,则要给计算机加驱动程序)
ct=DriverManager.getConnection("jdbc:odbc:sql sever Test");
//是windows进入,如果是密码的话后加:"用户名","密码"
sm=ct.createStatement();
/*
* 上面三个用来连接数据库
* */
//1.查找
ResultSet rs=sm.executeQuery("select*from bumen");
while(rs.next())
//rs.next():下一条记录,意为只要有下一条记录就继续执行,如果没有就退出 while语句
{
int bianhao=rs.getInt(1);
String mingcheng=rs.getString(2);
String didian=rs.getString(3);
System.out.println(bianhao+" "+mingcheng+" "+didian);
}
//2.添加
int i=sm.executeUpdate("insert into bumen values('12','aa','aaaa')");
if(i==0)
{
System.out.println("添 加成功!");
}else
{
System.out.println("添加失败!");
}
//3.删除
int j=sm.executeUpdate("delete from bumen where bianhao='8'");
if(j==0)
{
System.out.println("添 加成功!");
}else
{
System.out.println("添加失败!");
}
//4.修改
int k=sm.executeUpdate("update bumen set didian='忠义堂'");
if(k==0)
{
System.out.println("添 加成功!");
}else
{
System.out.println("添加失败!");
}
}catch(Exception e){}
finally
{
try{
if(sm!=null)
{
sm.close();
}
if(ct!=null)
{
ct.close();
}
}catch(Exception e2){}
}
}
}
import java.sql.*;
public class TTest1 {
public static void main(String[]args){
PreparedStatement ps=null;//用来弥补漏洞
Connection ct=null;
ResultSet rs=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
ct=DriverManager.getConnection("jdbc:odbc:sql sever Test");
//1.插入
/* ps=ct.prepareStatement("insert into bumen values(?,?,?)");
ps.setInt(1, 7); ps.setString(2,"侦察");
ps.setString(3, "山外");
*/
//1,2,3是第几个问号的意思
//2.查询
ps=ct.prepareStatement("select*from bumen where bianhao=? or didian=?");
ps.setInt(1,6); ps.setString(2,"后山");
rs=ps.executeQuery();
while(rs.next())
{
int bianhao=rs.getInt(1);
String mingcheng=rs.getString(2);
String didian=rs.getString(3);
System.out.println(bianhao+" "+mingcheng+" "+didian);
}
}catch(Exception e){}
finally
{
try{
if(rs!=null)
{
rs.close();
}
if(ps!=null)
{
ps.close();
}
}catch(Exception e2){}
}
}
}
import java.sql.*;
public class TTest1 {
public static void main(String[]args){
PreparedStatement ps=null;//用来弥补漏洞
Connection ct=null;
ResultSet rs=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
ct=DriverManager.getConnection("jdbc:odbc:sql sever Test");
ps=ct.prepareStatement("create database abcd");
ps=ct.prepareStatement("create table aabbcc(aa int,bb nvarchar(2))");
ps=ct.prepareStatement("backup database abcd to disk='e:/abcd.bak'");
//备份文件
ps=ct.prepareStatement("drop table aabbcc");
ps=ct.prepareStatement("restore database abcd from disk='e:abcd.bak'");
//从备份文件中取出
ps.execute();
System.out.println("执行完毕!");
}catch(Exception e){}
finally
{
try{
if(rs!=null)
{
rs.close();
}
if(ps!=null)
{
ps.close();
}
}catch(Exception e2){}
}
}
}