java-连接数据库(SQL server 2012)

       用Java写一个阅读器,想着要保存设置的字体、背景颜色,就连接了数据库,以下是这部分的代码:

//TestDB类的功能是连接数据库MyNote,包括对表note的查询、插入、删除等数据操作。
public class TestDB {  
  
String result=null;

        // 声明JDBC对象  
       Connection con = null;  
       Statement stmt = null;  
       ResultSet rs = null;  


public TestDB(){
       try {  
        // 设置连接数据库的各个参数.  
           @SuppressWarnings("unused")
String connectionUrl = "jdbc:sqlserver://localhost:1433;"  
                   + "databaseName=AdventureWorks;integratedSecurity=true;";  
           String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=MyNote;user=sa;password=gxu";//以sa身份连接数据库 
           // 建立数据库连接.  
           System.out.println("尝试建立数据库连接.");  
           Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");   
           con = DriverManager.getConnection(url);  
           System.out.println("数据库连接成功!");  
         }
         //处理和打印程序异常  
       catch (Exception e) {  
           e.printStackTrace();  
       }   
       }

public Vector<String> selectsql(String filename){
Vector<String> v=new Vector<String>();
try {  
//创建SQL语句查询student表中的所有记录并返回查询结果
         String SQL = "SELECT * FROM note WHERE filepath='"+filename+"';";  
         stmt = con.createStatement();
         rs=stmt.executeQuery(SQL);
         while (rs.next()) {
        for(int i=0;i<10;i++)
        {
        result=rs.getString(i+1);
        v.add(result); 
        }
         }
         /*
         //遍历和打印SQL语句的查询结果
         System.out.println("文件路径"+"\t\t"+"字体"+"\t"+"字形"+"\t"+"字号"+"\t"+"R"+"\t"+"G"+"\t"+"B"+"\t");  
         while (rs.next()) {
             System.out.println(rs.getString("filepath")+"\t\t"+rs.getString("fontname")+"\t"
             +rs.getString("fontstyle")+"\t"+rs.getString("fontsize")+"\t"+rs.getString("fontcolorR")+"\t"
              +rs.getString("fontcolorG")+"\t"+rs.getString("fontcolorB"));
         }
         */
  }
       //处理和打印程序异常  
  catch (Exception e) {  
        e.printStackTrace();  
  } 
 return v;  
}
 
public void createsql(){
try {  
//创建表note的SQL语句
String SQL = "CREATE TABLE note "+
                 "(filepath varchar(225) not NULL, "+
     "fontname varchar(20), "+
                 "fontstyle smallint, "+
     "fontsize smallint, "+
                 "fontcolorR smallint, "+
                 "fontcolorG smallint, "+
                 "fontcolorB smallint, "+
                 "backcolorR smallint, "+
                 "backcolorG smallint, "+
                 "backcolorB smallint, "+
                 "PRIMARY KEY (filepath))";  
        stmt = con.createStatement();  
        stmt.executeUpdate(SQL);
  }
      //处理和打印程序异常  
  catch (Exception e) {  
           e.printStackTrace();  
  }  
}
public void deletesql(String filename){
try {  
String SQL = "DELETE FROM note WHERE filepath='"+filename+"';";  
        stmt = con.createStatement();  
        stmt.executeUpdate(SQL);
  }
      //处理和打印程序异常  
  catch (Exception e) {  
           e.printStackTrace();  
  }  
 
}
 
public void insertsql(String filepath,String fontname,int fontstyle,int fontsize,int fontcolorR,int fontcolorG,int fontcolorB,int backcolorR,int backcolorG,int backcolorB){
try {  
//创建SQL语句向note表中插入一条记录
             String SQL = "INSERT INTO note(filepath,fontname,fontstyle,fontsize,fontcolorR,fontcolorG,fontcolorB,backcolorR,backcolorG,backcolorB) VALUES('"+filepath+"','"+fontname+"',"+fontstyle+","+fontsize+","+fontcolorR+","+fontcolorG+","+fontcolorB+","+backcolorR+","+backcolorG+","+backcolorB+");"; 
             stmt.executeUpdate(SQL);
  }
      //处理和打印程序异常  
  catch (Exception e) {  
           e.printStackTrace();  
  }  

}
public void off(){ 
         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) {  }    
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值