Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException; Import Team.
Jdbctest;
public class Dbcstudent {//define database access parameter String URL = "Jdbc:sqlserver://localhost:1433;databasename=jdbctest";
String user = "sa";
String password = "AAAAAA";
static String drivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
Connection Conn;
PreparedStatement PS;
ResultSet rs;
String id= "";
String name= "";
int age=0;
String sex= "";
1, load driver static {try {class.forname (drivername);
catch (ClassNotFoundException e) {System.out.println ("Drive load Failed"); }//2, create Connection object public Connection getconnection () throws sqlexception{Conn=drivermanager.getconnection (Url,user,pas
Sword);
Return conn;
} public void Add (String id,string name,int age,string Sex) throws ClassNotFoundException, SQLException {//define SQL statement String sql= INSERT into Studentmanage (studentid,studentnAme,age,sex) VALUES (?,?,?,?) ";
3, create the Statement object Ps=getconnection (). preparestatement (SQL);
Ps.setstring (1,id);
Ps.setstring (2,name);
Ps.setint (3,age);
Ps.setstring (4,sex);
Ps.executeupdate ();
4, traversal result set: Insert records here do not need//5, Close Resource object Ps.close ();
Getconnection (). Close (); }/*public void Update (String id,string name,int age,string Sex) throws ClassNotFoundException, SQLException {//Set Semantic SQL statement String sql= "UPDATE studentmanage SET studentname=?,age=?,sex=?"
where studentid=? ";
3, create the Statement object Ps=getconnection (). preparestatement (SQL);
Ps.setstring (1,name);
Ps.setint (2,age);
Ps.setstring (3,sex);
Ps.setstring (4,id);
Ps.executeupdate ();
4, traversal result set: Insert records here do not need//5, Close Resource object Ps.close ();
Getconnection (). Close (); public void Delete (String id) throws ClassNotFoundException, SQLException {//define SQL statement string sql= "Delete S
Tudentmanage WHERE studentid=? "; 3, create the statement object Ps=getconnectIon (). preparestatement (SQL);
Ps.setstring (1,id);
4, traversal result set: Insert records here do not need//5, Close Resource object Ps.close ();
Getconnection (). Close (); }*/Public String get (string id) throws ClassNotFoundException, SQLException {//define SQL statement string sql= "select S
Tudentid,studentname,age,sex from Studentmanage WHERE studentid=? ";
3, create the Statement object Ps=getconnection (). preparestatement (SQL);
Ps.setstring (1,id);
ResultSet rs = Ps.executequery ();
while (Rs.next ()) {id=rs.getstring ("StudentID");
Name=rs.getstring ("Studentname");
Age=rs.getint ("Age");
Sex=rs.getstring ("Sex");
//4, traverse result set: Insert record here does not need//5, Close Resource object Ps.close ();
Getconnection (). Close ();
Return "School Number:" +id+ "Name:" +name+ "Age:" +age+ "Sex:" +sex; public void Get () throws ClassNotFoundException, SQLException {} public static void Main (string[) args) throws Cla
ssnotfoundexception,sqlexception {//jdbctest jt=new jdbctest ();
Jt.add (); //jt.update ();
Jt.delete ();
}
}