package com.company.dao;
创新互联是一家专注于成都网站建设、成都做网站与策划设计,忻府网站建设哪家好?创新互联做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:忻府等地区。忻府做网站价格咨询:18980820575
import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class BaseDao {
// 数据库驱动
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//url
String url = "jdbc:sqlserver://数据库ip:端口号;databaseName=数据库名;";
//用户名
String uname = "数据库用户名";
//密码
String pwd = "数据库密码";
/**
* 获得连接对象
* @return
*/
protected Connection getCon(){
//返回的连接
Connection con = null;
try {
//载入驱动
Class.forName(driver);
//得到连接
con = DriverManager.getConnection(url, uname, pwd);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
/**
* 关闭数据库
* @param con
* @param stmt
* @param rs
*/
protected void closeDB(Connection con, Statement stmt, ResultSet rs){
if(rs != null){
try {
//关闭结果集
rs.close();
rs = null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(stmt != null){
try {
//关闭语句对象
stmt.close();
stmt = null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(con != null){
try {
//关闭连接对象
con.close();
con = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
protected void closeDB(Connection con, PreparedStatement pstmt, ResultSet rs){
if(rs != null){
//关闭结果集
try {
rs.close();
rs = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pstmt != null){
try {
pstmt.close();
pstmt = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
con = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
这个是我写的一个基本的连接sql2005数据库的代码,.! 不知道你能不能用,! 你看一下吧, 连接的时候需要sqljdbc.jar数据库驱动,!
算是最简单的吧
package cn.job01;
import java.util.Scanner;
public class Lx07 {
public static void choice() {
System.out.println("登陆菜单 ");
System.out.println("1登陆系统");
System.out.println("2退出");
}
static void choice1() {
System.out.println("购物管理系统客户信息");
System.out.println("1显示所有客户信息");
System.out.println("2添加客户信息");
System.out.println("3修改客户信息");
System.out.println("4查询客户信息");
}
static void choice2() {
System.out.println("购物管理系统真情回馈");
System.out.println("1幸运大放送");
System.out.println("2幸运抽奖");
System.out.println("3生日问候");
}
public static void main(String[] args) {
choice();
Scanner input = new Scanner(System.in);
System.out.println("请输入1or2");
int num = input.nextInt();
switch (num) {
case 1:
System.out.println("主菜单");
System.out.println("1客户信息管理");
System.out.println("2购物结算");
System.out.println("3真情回馈");
System.out.println("4注销");
break;
}
System.out.println("选择输入数字");
int num1 = input.nextInt();
switch (num1) {
case 1:
choice1();
break;
case 2:
System.out.println("购物结算");
break;
case 3:
choice2();
break;
case 4:
choice();
break;
}
}
}
一)[DLS_DEAD_LOCAL_STORE]
描述: Dead store to 未使用的局部变量
解决方法:局部变量定义后未使用;实例化对象后又重新对该对象赋值
(二) [ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD]
描述:Write to static field 通过实例方法更新静态属性
常见于常量类,直接通过类名.常量名获取的方式违背了封装的原则,findbugs不提倡使用,而如果将常量改成静态成员变量,又因为spring不支持静态注入导致不能实现,解决方法是非静态的setter调用静态的setter方法给静态成员变量赋值。
解决方法:
常量类F:
class F{
public static String a = “123”;
}
常量a改为静态成员变量,通过F.getA()获取,且由于spring不支持静态注入,改为:
class F{
private static String a;
public static Integer getA() {
return a;
}
public void setA(String a) {
setAValue(a);
}
public static void setAValue(String a) {
F.a = a;
}
}
(三) [BX_UNBOXING_IMMEDIATELY_REBOXED]
描述: Boxed value is unboxed and then immediately reboxed 装箱的值被拆箱,然后立刻重新装箱了
常见的是三目运算时,同时存在基本类型和包装类型。
解决方法:
Integer a = null;
//...
a = (a == null)?0:a;
此问题在于a不为null时,会被拆箱,赋值时再装箱。这是自动装箱拆箱的特性,只要运算中有不同类型,当涉及到类型转换时,编译器就会向下转型,再进行运算。修改方法,统一类型:
Integer a = null;
//...
a = (a == null)?Integer.valueOf(0):a;
(四) [SE_BAD_FIELD]
描述: Non-transient non-serializable instance field in serializable class在可序列化的类中存在不能序列化或者不能暂存的数据
解决方法:
方法1:序列化该对象
方法2:当采用struts2框架开发,不可避免的此问题会大量出现,因为ActionSupport实现了序列化接口,action继承了此类,而 service没序列化,所以在action中引用service对象时提示此错误,最简单的解决方法是将service对象声明成transient, 即service不需要序列化
方法3(未验证):To avoid java serialization you need to implement writeObject() and readObject() method in your Class and need to throw NotSerializableExceptionfrom those method.(action中实现这两个方法?)
private void writeObject(java.io.ObjectOutputStream stream) throws java.io.IOException {
throw new java.io.NotSerializableException( getClass().getName() );
}
private void readObject(java.io.ObjectInputStream stream) throws java.io.IOException, ClassNotFoundException {
throw new java.io.NotSerializableException( getClass().getName() );
}
(五) [NP_LOAD_OF_KNOWN_NULL_VALUE]
描述: Load of known null value加载已知是null的值
解决方法:已知方法参数为null是,直接传递null而不是参数名
(六) [REC_CATCH_EXCEPTION]
描述: Exception is caught when Exception is not thrown 过泛地捕获异常或捕获异常后未做任何处理
解决方法:异常分类捕获(至少要打印出此异常对象)
(七) [NP_NULL_PARAM_DEREF]
描述: Null passed for nonnull parameter 把空值传给了非空的参数
解决方法:增加非空判断
(八) [NP_IMMEDIATE_DEREFERENCE_OF_READLINE]
描述: Immediate dereference of the result of readLine() 立即引用了readLine()的结果
解决方法:判断readLine的结果是否为空
(九) [EI_EXPOSE_REP] 恶意代码漏洞
描述:may expose internal representation by returning getter方法返回引用类型
eclipse自动生成的引用类型(Object、数组、Date等)的getter、setter方法会得到或通过对可变对象的引用操作而暴露代码内部实现,解决方法很多,只要返回的或赋值的对象不是原引用对象即可。
解决方法:
以Date类型为例:
public Date getHappenTime() {
if(happenTime != null){
return (Date) happenTime.clone();
}
return null;
}
(十) [ EI_EXPOSE_REP2] 恶意代码漏洞
描述:may expose internal representation by storing an externally mutable object into setter方法返回引用类型
eclipse自动生成的引用类型(Object、数组、Date等)的getter、setter方法会得到或通过对可变对象的引用操作而暴露代码内部实现,解决方法很多,只要返回的或赋值的对象不是原引用对象即可。
解决方法:
以Date类型为例:
public void setHappenTime(Date happenTime) {
if(happenTime != null){
this.happenTime = (Date) happenTime.clone();
}else{
this.happenTime = null;
}
}
package untitled5;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.lang.*;
import javax.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class delbook extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel4 = new JLabel();
JTextField jTextField2 = new JTextField();
JLabel jLabel5 = new JLabel();
JTextField jTextField3 = new JTextField();
JLabel jLabel6 = new JLabel();
JTextField jTextField4 = new JTextField();
JButton jButton1 = new JButton();
//Construct the frame
public delbook() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
jLabel1.setFont(new java.awt.Font("SansSerif", 0, 25));
jLabel1.setForeground(Color.red);
jLabel1.setText("超市管理系统");
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(500,400));
this.setTitle("超市管理系统");
jLabel2.setFont(new java.awt.Font("SansSerif", 0, 30));
jLabel2.setText("业务单位信息");
jLabel3.setFont(new java.awt.Font("SansSerif", 0, 25));
jLabel3.setText("产品编号");
jTextField1.setText("");
jLabel4.setFont(new java.awt.Font("SansSerif", 0, 25));
jLabel4.setText("公司名称");
jTextField2.setText("");
jLabel5.setFont(new java.awt.Font("SansSerif", 0, 25));
jLabel5.setText("订单号码");
jTextField3.setText("");
jLabel6.setFont(new java.awt.Font("SansSerif", 0, 25));
jLabel6.setText("电 话");
jTextField4.setText("");
jButton1.setFont(new java.awt.Font("SansSerif", 0, 25));
jButton1.setText("提交");
jButton1.addActionListener(new delbook_jButton1_actionAdapter(this));
contentPane.add(jLabel1, new XYConstraints(179, 1, 153, 32));
contentPane.add(jLabel2, new XYConstraints(162, 33, -1, -1));
contentPane.add(jLabel3, new XYConstraints(83, 89, -1, -1));
contentPane.add(jTextField1, new XYConstraints(189, 88, 141, 36));
contentPane.add(jTextField2, new XYConstraints(189, 149, 142, 36));
contentPane.add(jLabel4, new XYConstraints(84, 148, -1, -1));
contentPane.add(jTextField3, new XYConstraints(188, 206, 143, 33));
contentPane.add(jLabel5, new XYConstraints(84, 204, -1, -1));
contentPane.add(jLabel6, new XYConstraints(84, 253, -1, -1));
contentPane.add(jTextField4, new XYConstraints(189, 260, 143, 36));
contentPane.add(jButton1, new XYConstraints(197, 318, -1, -1));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void update() {
try {
//定义显示的字符串
String str1;
String str2;
String str3;
String str4;
str1 = jTextField1.getText();
str2 = jTextField2.getText();
str3 = jTextField3.getText();
str4 = jTextField4.getText();
//装载jdbc驱动程序
String driverName = "oracle.jdbc.OracleDriver";
Driver driver = (Driver) Class.forName(driverName).newInstance();
//连接数据库
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@thsspc0791:1521:liuyong", "hr", "tongfang");
PreparedStatement pstmt = con.prepareStatement(
" insert Customer1('goodID','Name','PID','tel')values(?,?,?,?)");
pstmt.setString(1, str1);
pstmt.setString(2, str2);
pstmt.setString(1, str3);
pstmt.setString(4, str4);
ResultSet res = pstmt.executeQuery();
pstmt.close();
con.close();
}catch (InstantiationException e) {
System.out.println(e.getMessage());
}catch (IllegalAccessException e) {
System.out.println(e.getMessage());
}catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}catch (SQLException edd) {
edd.printStackTrace() ;
System.out.println(edd.getMessage());
}
}
void jButton1_actionPerformed(ActionEvent e) {
update();
}
}
class delbook_jButton1_actionAdapter implements java.awt.event.ActionListener {
delbook adaptee;
delbook_jButton1_actionAdapter(delbook adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
package untitled5;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
/**
* pTitle: /p
* pDescription: /p
* pCopyright: Copyright © 2003/p
* pCompany: /p
* @author not attributable
* @version 1.0
*/
public class retur extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JLabel jLabel1 = new JLabel();
//Construct the frame
public retur() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
jLabel1.setFont(new java.awt.Font("SansSerif", 0, 20));
jLabel1.setForeground(Color.red);
jLabel1.setText("超市管理系统");
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("超市管理系统");
contentPane.add(jLabel1, new XYConstraints(139, 1, 126, 33));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}
package untitled5;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.lang.*;
import javax.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import com.borland.dbswing.*;
//货品信息登记
public class Frame2 extends JFrame {
JPanel contentPane;
JLabel jLabel1 = new JLabel();
XYLayout xYLayout1 = new XYLayout();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
JTextField jTextField1 = new JTextField();
JLabel jLabel4 = new JLabel();
JTextField jTextField2 = new JTextField();
JPanel jPanel1 = new JPanel();
XYLayout xYLayout2 = new XYLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JLabel jLabel5 = new JLabel();
JTextField jTextField3 = new JTextField();
//Construct the frame
public Frame2() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(600, 500));
this.setTitle("超市管理系统");
this.addHierarchyBoundsListener(new Frame2_this_hierarchyBoundsAdapter(this));
jLabel1.setFont(new java.awt.Font("SansSerif", 0, 25));
jLabel1.setForeground(Color.red);
jLabel1.setText("超市管理系统");
contentPane.setForeground(Color.black);
jLabel2.setFont(new java.awt.Font("SansSerif", 0, 30));
jLabel2.setText("产 品 信 息 展 示");
// statusBar.setFont(new java.awt.Font("SansSerif", 0, 20));
jLabel3.setFont(new java.awt.Font("SansSerif", 0, 20));
jLabel3.setText("产品名称");
jTextField1.setText("");
jLabel4.setEnabled(true);
jLabel4.setFont(new java.awt.Font("SansSerif", 0, 20));
jLabel4.setText("产品ID号");
jTextField2.setText("");
jTextField2.addActionListener(new Frame2_jTextField2_actionAdapter(this));
jPanel1.setLayout(xYLayout2);
jLabel5.setFont(new java.awt.Font("SansSerif", 0, 25));
jLabel5.setForeground(Color.red);
jLabel5.setText("该产品详细信息");
jTextField3.setText("");
contentPane.add(jLabel1, new XYConstraints(237, 0, 153, 40));
contentPane.add(jLabel2, new XYConstraints(200, 47, 231, 58));
contentPane.add(jLabel3, new XYConstraints(47, 102, 101, 42));
contentPane.add(jTextField1, new XYConstraints(128, 108, 112, 34));
contentPane.add(jTextField2, new XYConstraints(361, 107, 109, 36));
contentPane.add(jPanel1, new XYConstraints(75, 166, 453, 277));
jPanel1.add(jScrollPane1, new XYConstraints(14, 8, 433, 221));
jScrollPane1.getViewport().add(jTextField3, null);
jPanel1.add(jLabel5, new XYConstraints(112, 240, -1, -1));
contentPane.add(jLabel4, new XYConstraints(278, 111, -1, -1));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void Select() {
try {
String str1, str2;
str1 = jTextField1.getText();
str2 = jTextField2.getText();