1.首先加载驱动 Driver
- 01.打出SQLServerDriver,按快捷
- 02.使用Class.forName加载驱动
-
1 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 2.获得连接
- 编写链接语句
-
String url = "jdbc:sqlserver://localhost:1433;databasename=api_284";
- 02.得到连接
-
Connection con = DriverManager.getConnection(url, "sa", "sa123");
3.执行指令
- 01.获得执行对象
-
PreparedStatement ps = con.prepareStatement("insert into student values(?,?,?)"); 02.让执行对象执行
-
Scanner mys=new Scanner(System.in);
System.out.println("请输入你的名字");
String name=mys.next();
System.out.println("请输入你的年龄");
int age=mys.nextInt();
Date brithday=new Date(System.currentTimeMillis());
?代表占位符,一个占位符意味着需要一个变量来填充
int n = ps.executeUpdate();// n就是数据库收到影响的行数
// 怎么判断是否操作成功
System.out.println(n>0?"成功":"失败");
4.最后关闭资源
-
if(con!=null&&!con.isClosed()) {
con.close();
}
if(ps!=null) {
ps.close();
}
1 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 2.获得连接
String url = "jdbc:sqlserver://localhost:1433;databasename=api_284";
Connection con = DriverManager.getConnection(url, "sa", "sa123");
3.执行指令
PreparedStatement ps = con.prepareStatement("insert into student values(?,?,?)"); 02.让执行对象执行
Scanner mys=new Scanner(System.in);
System.out.println("请输入你的名字");
String name=mys.next();
System.out.println("请输入你的年龄");
int age=mys.nextInt();
Date brithday=new Date(System.currentTimeMillis());
?代表占位符,一个占位符意味着需要一个变量来填充
int n = ps.executeUpdate();// n就是数据库收到影响的行数
// 怎么判断是否操作成功
System.out.println(n>0?"成功":"失败");
4.最后关闭资源
if(con!=null&&!con.isClosed()) {
con.close();
}
if(ps!=null) {
ps.close();
}



