1.需求分析
在數據庫學習當中,我接觸到了一款新的數據庫:SQL SERVER數據庫;它與MYSQL相比有更強大的功能,而且連接
方式和MYSQL大同小異,今天就總結一下它的連接方法。
2.前期准備
(1)在連接本地sqlserver數據庫之前,我們要先啟動它的TCP/IP協議:
在sql server配置管理器中找到MSSQLSERVER的協議,打開TCP/IP協議
確認ip1的端口是否為1443;完成后通過鼠標右鍵啟動TCP/IP協議。
(2)導入jar包
在項目中導入msbase.jar, mssqlserver.jar, msutil.jar
jar包下載網址https://mvnrepository.com/
3.連接代碼
增:
//表單提交的數據
request.setCharacterEncoding("UTF-8");
String name= request.getParameter("name");
String num=request.getParameter("num");
String sex= request.getParameter("sex");
String birth= request.getParameter("birth");
String address= request.getParameter("address");
Connection connection =null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student";//DatabaseName后是要訪問的數據庫名
String user="sa";
String password=""; //登錄密碼
connection= DriverManager.getConnection(url,user,password);
if(connection!=null)
{
//out.print("Success connect MySql server!");
}
else
{
out.print("fail connect MySql server!");
}
} catch (Exception e) {
out.print(" connect MySql server wrong!");
}
String sql = "insert into tablename(num,name,sex,birth,address) values(?,?,?,?,?)";//tablename為要訪問的表名,value前面括號中是要插入的列名,后面是具體值
PreparedStatement ps = null;
boolean flag = false;
int a = 0;
//下面num,name,sex,birth,address為定義的變量,我的是通過上面表單提交獲取的
ps = connection.prepareStatement(sql);
ps.setString(1, num);
ps.setString(2, name);
ps.setString(3, sex);
ps.setString(4, birth);
ps.setString(5, address);
a = ps.executeUpdate();//a用來判斷操作是否成功,若a!=0則表示操作成功
刪:
//與增相似,注解同上
request.setCharacterEncoding("UTF-8");
String id=request.getParameter("id");
Connection connection =null;
String db_url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student";
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student";
String user="sa";
String password="";
connection= DriverManager.getConnection(url,user,password);
if(connection!=null)
{
//out.print("Success connect MySql server!");
}
else
{
out.print("fail connect MySql server!");
}
} catch (Exception e) {
out.print(" connect MySql server wrong!");
}
String sql = "delete from students where id =" + id+ " ";
Statement stmt=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
int a = 0;
a = stmt.executeUpdate(sql);
改:
//注解同上
request.setCharacterEncoding("UTF-8");
String id=request.getParameter("id");
String name= request.getParameter("name");
String num=request.getParameter("num");
String sex= request.getParameter("sex");
String birth= request.getParameter("birth");
String address= request.getParameter("address");
Connection connection =null;
String db_url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student";
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student";
String user="sa";
String password=""; //
connection= DriverManager.getConnection(url,user,password);
if(connection!=null)
{
//out.print("Success connect MySql server!");
}
else
{
out.print("fail connect MySql server!");
}
} catch (Exception e) {
out.print(" connect MySql server wrong!");
}
String sql="update students set num='" + num+ "',name='"+name+"',sex='"+sex+"',birth='"+birth+"',address='"+address+"' where id="+id+"";
Statement stmt=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
int a=0;
a = stmt.executeUpdate(sql);
查:
//注解同上
Connection connection=null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String db_url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=student";
String user="sa";
String password=""; //
connection= DriverManager.getConnection(db_url,user,password);
if(connection!=null)
{
//out.print("Success connect MySql server!");
}
else
{
out.print("fail connect MySql server!");
}
} catch (Exception e) {
out.print(" connect MySql server wrong!");
}
Statement stmt=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = null;
String sql = " select * from students";
rs = stmt.executeQuery(sql);
//此時我們獲取的數據存在rs中我們可根據需求通過
while(rs.next())
{
/*
你需要的方法
*/
}
進一步操作數據
4.總結:
sqlserver數據庫的操作與mysql有很大的相似之處,正是因為我對mysql數據庫操作的熟練掌握才能讓我在這次實驗中
沒有走彎路;所以初學某類事物時一定不要淺嘗輒止。