package com.cn.test2;
import java.sql.*;
public class jdbc{
public static void main (String args[]){
Connection ct=null;
PreparedStatement ps=null;
ResultSet rs=null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename=master","sa","1");
ps=ct.prepareStatement("insert into stu values (?,?,?,?,?,?)");
ps.setString(1, "sp110");
ps.setString(2, "霍少");
ps.setString(3, "男");
ps.setFloat(4, 24);
ps.setString(5, "长沙");
ps.setString(6,"大学");
ps.executeUpdate();
ps=ct.prepareStatement("select * from stu");
rs=ps.executeQuery();
while (rs.next()){
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(6));
}
ps=ct.prepareStatement("update stu set stuName=? where stuAge=?");
ps.setString(1, "霍少爷");
ps.setFloat(2,24);
ps.executeUpdate();
ps=ct.prepareStatement("select * from stu");
rs=ps.executeQuery();
while (rs.next()){
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(6));
}
}catch (Exception e){
e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
rs=null;
}
if(ps!=null){
ps.close();
ps=null;
}
if(ct!=null){
ct.close();
ct=null;
}
}catch(Exception e){
e.printStackTrace();
}
}
}
}
JDBC调用数据库 插入操作
最新推荐文章于 2023-10-01 18:17:36 发布