private void copyFile() throws Exception { // Load the JDBC driver Class.forName(((String) jcboDriver.getSelectedItem()).trim()); System.out.println("Driver loaded"); // Establish a connection Connection conn = DriverManager.getConnection( ((String) jcboURL.getSelectedItem()).trim(), jtfUsername.getText().trim(), String.valueOf(jtfPassword.getPassword()).trim()); System.out.println("Database connected"); // Read each line from the text file and insert it to the table insertRows(conn); }
public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == bRes) { tname.setText(""); textra.setText(""); } else { if (it.isSelected()) field = 1; else if (civil.isSelected()) field = 2; else if (mech.isSelected()) field = 3; if (tname.getText().equals("") | textra.getText().equals("") | field == 0) JOptionPane.showMessageDialog(null, "Please Fill in All Entries!!"); else { String sal = (String) cbsal.getSelectedItem(); try { // Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Connection conn=DriverManager.getConnection("jdbc:odbc:go"); Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql:///go", "root", ""); Statement pst = conn.createStatement(); pst.executeUpdate( "Insert into company values('" + tname.getText() + "','" + textra.getText() + "','" + (String) sal + "','" + field + "','" + tusr.getText() + "','" + tpwd.getText() + "')"); conn.close(); String msg = "Your Details are Stored. Login again to View Related applicants! Thank You!!"; JOptionPane.showMessageDialog(null, msg); setVisible(false); login ab = new login(); } catch (Exception exc) { JOptionPane.showMessageDialog(null, tname.getText() + " : " + exc); System.exit(0); } } } }
public void actionPerformed(ActionEvent ae) { String str = ae.getActionCommand(); if (str.equals("Ok")) { String str1 = (String) jcmname.getSelectedItem(); Connection con = null; Statement stat = null; if (str1.equals("Pulsar") || str1.equals("CT 100") || str1.equals("Discover DTS-i") || str1.equals("Wave DTS-i")) { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("Jdbc:Odbc:showroom"); System.out.println("Got Connection :" + con); stat = con.createStatement(); ResultSet rs = stat.executeQuery("select * from vehicle"); System.out.println("chk1"); while (rs.next()) { if (str1.equals(rs.getString(1))) { tfcap.setText("" + rs.getInt(2)); tfeng.setText("" + rs.getInt(3)); tfbhp.setText("" + rs.getInt(4)); tfvolt.setText("" + rs.getInt(5)); tfrpm.setText("" + rs.getInt(6)); tfweight.setText("" + rs.getInt(7)); tfgear.setText("" + rs.getInt(8)); } } stat.close(); con.close(); } catch (Exception ex) { } } else { JOptionPane.showMessageDialog( null, "Please Choose Model Name", "Error", JOptionPane.ERROR_MESSAGE); } } if (str.equals("can")) { this.dispose(); // new menu(1); } }
private void setamount() { try { ResultSet rst = DBConnection.getDBConnection() .createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE) .executeQuery( "SELECT Amount FROM BOOKING where Pass_No='" + combo1.getSelectedItem() + "'"); while (rst.next()) { combo8.addItem(rst.getString(1)); } } catch (Exception n) { n.printStackTrace(); } }
public void actionPerformed(ActionEvent evt) { String arg = evt.getActionCommand(); if (arg.equals("Query")) { // 用户按下Query按钮 ResultSet rs = null; try { String author = (String) authors.getSelectedItem(); String publisher = (String) publishers.getSelectedItem(); if (!author.equals("Any") && !publisher.equals("Any")) { if (authorPublisherQueryStmt == null) { // 根据用户选择的出版社名和作者名查询相关的书名和书价 String authorPublisherQuery = "SELECT Books.Price, Books.Title " + "FROM Books, BooksAuthors, Authors, Publishers " + "WHERE Authors.Author_Id = BooksAuthors.Author_Id AND " + "BooksAuthors.ISBN = Books.ISBN AND " + "Books.Publisher_Id = Publishers.Publisher_Id AND " + "Authors.Name = ? AND " + "Publishers.Name = ?"; authorPublisherQueryStmt = con.prepareStatement(authorPublisherQuery); } authorPublisherQueryStmt.setString(1, author); authorPublisherQueryStmt.setString(2, publisher); rs = authorPublisherQueryStmt.executeQuery(); } else if (!author.equals("Any") && publisher.equals("Any")) { if (authorQueryStmt == null) { // 根据用户选择的作者名查询相关的书名和书价 String authorQuery = "SELECT Books.Price, Books.Title " + "FROM Books, BooksAuthors, Authors " + "WHERE Authors.Author_Id = BooksAuthors.Author_Id AND " + "BooksAuthors.ISBN = Books.ISBN AND " + "Authors.Name = ?"; authorQueryStmt = con.prepareStatement(authorQuery); } authorQueryStmt.setString(1, author); rs = authorQueryStmt.executeQuery(); } else if (author.equals("Any") && !publisher.equals("Any")) { if (publisherQueryStmt == null) { // 根据用户选择的出版社名查询相关的书名和书价 String publisherQuery = "SELECT Books.Price, Books.Title " + "FROM Books, Publishers " + "WHERE Books.Publisher_Id = Publishers.Publisher_Id AND " + "Publishers.Name = ?"; publisherQueryStmt = con.prepareStatement(publisherQuery); } publisherQueryStmt.setString(1, publisher); rs = publisherQueryStmt.executeQuery(); } else { if (allQueryStmt == null) { // 若用户未选任何信息,则输出所有的书名和对应的书价 String allQuery = "SELECT Books.Price, Books.Title FROM Books"; allQueryStmt = con.prepareStatement(allQuery); } rs = allQueryStmt.executeQuery(); } result.setText(""); while (rs.next()) result.append(rs.getString(1) + " | " + rs.getString(2) + "\n"); rs.close(); } catch (Exception e) { result.setText("Error " + e); } } else if (arg.equals("Change prices")) { // 用户选择“Change prices”按钮 String publisher = (String) publishers.getSelectedItem(); if (publisher.equals("Any")) result.setText("I am sorry, but I cannot do that."); else try { // 根据用户输入的新的书价更新Books表的数据 String updateStatement = "UPDATE Books " + "SET Price = Price + " + priceChange.getText() + " WHERE Books.Publisher_Id = " + "(SELECT Publisher_Id FROM Publishers WHERE Name = '" + publisher + "')"; int r = stmt.executeUpdate(updateStatement); result.setText(r + " records updated."); } catch (Exception e) { result.setText("Error " + e); } } }