package operationsqlserver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class TestSqlServerMain {
public static void main(String[] args) throws Exception {
// 使用sqlserver身份连接
String urlserver = "jdbc:sqlserver://127.0.0.1:1433;databaseName=TestSql;user=sa;password=123456";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 建立连接
System.out.println("准备连接!!!");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(urlserver);
System.out.println("连接成功!!!");
String sql="insert into personinfo (id,name,age) values (?,?,?)";
PreparedStatement pstm = con.prepareStatement(sql);
pstm.setInt(1, 1);
pstm.setString(2, "zhangsan");
pstm.setInt(3, 18);
pstm.executeUpdate();
System.out.println("插入成功...");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null)
try {
rs.close();
} catch (Exception e) {
}
if (stmt != null)
try {
stmt.close();
} catch (Exception e) {
}
if (con != null)
try {
con.close();
} catch (Exception e) {
}
}
}
}