问题描述
packagebuyticket;importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjavax.swing.table.TableColumn;importjavax.swing.*;publicclassMainFrameextendsJFrame{/****/privatestaticfinallongserialVersionUID=1L;JFramejf=newJFrame();//JPaneljp_title=newJPanel();JPaneljp_input=newJPanel();JPaneljp_showTicket=newJPanel();JPaneljp_buttons=newJPanel();//title//JLabellab_title=newJLabel("火车票订票系统");//填写内容JLabellab_sStation=newJLabel("始发站:");JLabellab_dStation=newJLabel("终点站:");JLabellab_sTime=newJLabel("开车时间:");JLabellab_trainID=newJLabel("车次");//内容/**JLabellab_turns=newJLabel("车次");JLabellab_sStations=new*JLabel("始发站");JLabellab_dStations=newJLabel("终点站");JLabel*lab_sTimes=newJLabel("开车时间");JLabellab_prices=newJLabel("票价");*/JTextFieldtf_sStation=newJTextField(10);JTextFieldtf_dStation=newJTextField(10);JTextFieldtf_sTime=newJTextField(10);JTextFieldtf_trainID=newJTextField(10);JTabletab_showTicket=newJTable();Objectdata[][]=null;String[]titles={"车次","车号","始发站","终点站","开车时间","票价","余票"};Stringa0,a1,a2,a3;JScrollPanejscrollPane=newJScrollPane(tab_showTicket);JButtonbtn_mesQuery=newJButton("购票信息查询");JButtonbtn_ticketQuery=newJButton("余票查询");publicMainFrame(Stringtitle){//jp_input.setLayout(newGridLayout(1,6));//jp_sercher.setLayout(new);//super(title);//jf.setLayout(newGridLayout(4,1));tab_showTicket=newJTable(data,titles);TableColumncolumn=null;for(inti=0;i<6;i++){column=tab_showTicket.getColumnModel().getColumn(i);if(i%2==0){column.setPreferredWidth(150);}else{column.setPreferredWidth(150);}}//JScrollPanescrollPane=newJScrollPane();//jp_title.add(lab_title);//jp_input添加组件jp_input.add(lab_sStation);jp_input.add(tf_sStation);jp_input.add(lab_dStation);jp_input.add(tf_dStation);jp_input.add(lab_trainID);jp_input.add(tf_trainID);jp_input.add(lab_sTime);jp_input.add(tf_sTime);//jp_sercher添加组件jp_buttons.add(btn_mesQuery);jp_buttons.add(btn_ticketQuery);//jp_showticket添加组件//jp_showTicket.add(scrollPane);//jf.add(jp_title);jf.add(jp_input,BorderLayout.NORTH);//jf.add(jp_sercher);jf.getContentPane().add(jscrollPane,BorderLayout.CENTER);jf.add(jp_buttons,BorderLayout.SOUTH);jf.setTitle("火车票查询系统");jf.setVisible(true);//jf.setBounds(200,200,800,600);jf.setSize(800,600);jf.setDefaultCloseOperation(EXIT_ON_CLOSE);//jf.setResizable(false);btn_mesQuery.addActionListener(newmyListener());btn_ticketQuery.addActionListener(newmyListener());}publicclassmyListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){if(e.getSource()==btn_mesQuery){}if(e.getSource()==btn_ticketQuery){StringdriverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";StringdbURL="jdbc:sqlserver://localhost:1433;DatabaseName=TicketManager";StringuserName="sa";StringuserPwd="123456";try{Class.forName(driverName);ConnectiondbConn=DriverManager.getConnection(dbURL,userName,userPwd);System.out.println("连接数据库成功");}catch(Exceptione1){e1.printStackTrace();System.out.print("连接失败");}Statementstmt=null;Connectionconn=null;ResultSetrs=null;try{conn=DriverManager.getConnection(dbURL);stmt=conn.createStatement();Stringsql="select*fromTicketwhereLeaveCitylike'";sql+=tf_sStation+"'";/*andArriveCity='"+tf_dStation+"';";sql+="'andLeaveTime="+tf_sTime+"'orTrainID="+tf_trainID+"'";*/rs=stmt.executeQuery(sql);intlr,lc;for(lr=0;lr<30;lr++){for(lc=0;lc<6;lc++){data[lr][lc]="";}inti=0;a0="'"+"%"+tf_sStation.getText().trim()+"%"+"'";a1="'"+"%"+tf_dStation.getText().trim()+"%"+"'";a2="'"+"%"+tf_sTime.getText().trim()+"%"+"'";a3="'"+"%"+tf_trainID.getText().trim()+"%"+"'";//根据给定条件,进行查询Stringtemp="select*fromTicketswhereLeaveCitylike"+a0+"andArriveCitylike"+a1+"andTrainIDlike"+a2+"andLeaveTimelike"+a3;rs=stmt.executeQuery(temp);//显示符合要求的所有图书信息while(rs.next()){data[i][0]=rs.getString(1);data[i][1]=rs.getString(2);data[i][2]=rs.getString(3);data[i][3]=rs.getString(4);data[i][4]=rs.getString(5);data[i][5]=rs.getString(6);i++;}}}catch(SQLExceptioned){System.out.println(ed);}}}}会出现错误连接数据库成功com.microsoft.sqlserver.jdbc.SQLServerException:用户''登录失败。ClientConnectionId:4a259d64-d6d2-4092-a8b7-9cf4e1300e2e还有JScrollPane添加有没有错误,一开始这里有错误的
解决方案
解决方案二:
换个SQL的jdbc驱动试试。
解决方案三:
conn=DriverManager.getConnection(dbURL);这句没有指定用户名和密码
解决方案四:
StringdbURL="jdbc:sqlserver://localhost:1433;DatabaseName=TicketManager";