package
com.wangguan;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJdbc {
public static void main(String[] args) {
Connection con = null ;
Statement sta = null ;
try {
Class.forName( " com.microsoft.sqlserver.jdbc.SQLServerDriver " );
String url = " jdbc:sqlserver://127.0.1:1433;databaseName=studentManager " ;
con = DriverManager.getConnection(url, " sa " , " wangguan " );
sta = con.createStatement();
String sql = " insert into student " +
" (stuName, address, stuTel, age, groupId) " +
" values " +
" ('王冠','黑龙江','13723984599',23,2) " ;
int row = sta.executeUpdate(sql);
if (row == 0 ) {
System.out.println();
} else {
System.out.println();
}
sta.close();
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (sta != null ) {
sta.close();
sta = null ;
}
if (con != null ) {
con.close();
con = null ;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestJdbc {
public static void main(String[] args) {
Connection con = null ;
Statement sta = null ;
try {
Class.forName( " com.microsoft.sqlserver.jdbc.SQLServerDriver " );
String url = " jdbc:sqlserver://127.0.1:1433;databaseName=studentManager " ;
con = DriverManager.getConnection(url, " sa " , " wangguan " );
sta = con.createStatement();
String sql = " insert into student " +
" (stuName, address, stuTel, age, groupId) " +
" values " +
" ('王冠','黑龙江','13723984599',23,2) " ;
int row = sta.executeUpdate(sql);
if (row == 0 ) {
System.out.println();
} else {
System.out.println();
}
sta.close();
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (sta != null ) {
sta.close();
sta = null ;
}
if (con != null ) {
con.close();
con = null ;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}