今天給大家帶來的是Java訪問Sql Server 2008 數據庫樣例,希望能幫助大家。
(1),文件列表
(2) java訪問sql server2008R2 設置 參考:
java開發相關資料 http://blog.csdn.net/keenweiwei/article/details/7328611
(3) 數據庫 表結構
(4)運行結果
(5) 完整java代碼下載: http://good.gd/1965614.htm
(DBHelper 抄的人家的,自己改了一部份)
1.test1.java
[java] view plaincopyprint?
import java.sql.ResultSet;
//import javax.swing.JOptionPane;
public class test11 {
public static void main(String[] args) {
DBHelper help=new DBHelper();
/* if(help.TestConn())
System.out.println("連接成功1");
else
System.out.println("連接失敗1");*/
//JOptionPane.showMessageDialog(null, "hello world");
//String sSQL="select * from w_brand where id=? and brand_name =?";
String sSQL="select * from w_brand where id=? and brand_name like '%{0}%'";
sSQL=sSQL.replace("{0}", "柔");
Object[] oParams=new Object[1];
oParams[0]=5;
//oParams[1]="柔";
/* ResultSet rs=help.GetResultSet(sSQL, oParams);
try
{
if(rs.next())
System.out.println("id: " + rs.getString(1) + " brand_name: " + rs.getString(2));
////索引從1開始
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}finally
{
try
{
rs.close();
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}*/
Object oResult=help.GetSingle(sSQL, oParams);
if(oResult!=null)
System.out.println(oResult.toString());
else
System.out.println("沒有符合條件的數據");
//DataTable dt=help.GetDataTable(sSQL,oParams);
/* String sSQL="select * from w_brand";
DataTable dt=help.GetDataTable(sSQL);*/
/* &