static { try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql:///hospital", "root", "root"); pa = con.prepareStatement("insert into patient values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) "); pd = con.prepareStatement("delete from patient where pid=? "); pl = con.prepareStatement("select * from patient order by pid"); pps = con.prepareStatement("select * from patient where pid=? "); pps1 = con.prepareStatement("select * from patient where plnm= ?"); } catch (Exception e) { System.out.println(e); } }
public boolean dbOpenList(Connection connection, String sql) { dbClearList(); this.oldSql = sql; this.oldConnection = connection; // apre il resultset da abbinare ResultSet resu = null; ResultSetMetaData meta; try { stat = connection.createStatement(); resu = stat.executeQuery(sql); meta = resu.getMetaData(); if (meta.getColumnCount() > 1) { this.contieneChiavi = true; } // righe while (resu.next()) { for (int i = 1; i <= meta.getColumnCount(); ++i) { if (i == 1) { dbItems.add((Object) resu.getString(i)); lm.addElement((Object) resu.getString(i)); } else if (i == 2) { dbItemsK.add((Object) resu.getString(i)); // debug // System.out.println("list:" + String.valueOf(i) + ":" + resu.getString(i)); } else if (i == 3) { dbItemsK2.add((Object) resu.getString(i)); } } } this.setModel(lm); // vado al primo if (dbTextAbbinato != null) { // debug // javax.swing.JOptionPane.showMessageDialog(null,this.getKey(0).toString()); dbTextAbbinato.setText(this.getKey(0).toString()); } return (true); } catch (Exception e) { javax.swing.JOptionPane.showMessageDialog(null, e.toString()); e.printStackTrace(); return (false); } finally { try { stat.close(); } catch (Exception e) { } try { resu.close(); } catch (Exception e) { } meta = null; } }
public void initDB(String url) { try { db = DriverManager.getConnection(url); statement = db.createStatement(); } catch (Exception e) { System.out.println("Database could not started"); e.printStackTrace(); } } // initDB sonu
public void closeDB() { try { if (statement != null) { statement.close(); } if (db != null) { db.close(); } } catch (Exception e) { System.out.println("Database could not closed"); e.printStackTrace(); } } // closeDB sonu
// 采用静态初始化块来初始化Connection、Statement对象 static { try { Properties props = new Properties(); props.load(new FileInputStream("mysql.ini")); String drivers = props.getProperty("driver"); String url = props.getProperty("url"); String username = props.getProperty("user"); String password = props.getProperty("pass"); // 加载数据库驱动 Class.forName(drivers); // 取得数据库连接 conn = DriverManager.getConnection(url, username, password); stmt = conn.createStatement(); } catch (Exception e) { e.printStackTrace(); } }
public static void mod(File x, String s) throws Exception { try { System.out.println(x); fis = new FileInputStream(x); // tmp=con.prepareStatement("update patient set // dt=?,pfnm=?,pmnm=?,plnm=?,gen=?,age=?,wt=?,addr=?,cno=?,dnnm=?,sym=?,dig=?,fee=? where // pid=1"); tmp = con.prepareStatement( "update patient set dt=?,pfnm=?,pmnm=?,plnm=?,gen=?,age=?,wt=?,addr=?,cno=?,dnm=?,sym=?,dig=?,fee=?,bg=?,i=?,path=? where pid=" + pid); // tmp.setString(1,pfnm); tmp.setString(1, "" + dt); tmp.setString(2, pfnm); tmp.setString(3, pmnm); tmp.setString(4, plnm); tmp.setString(5, gen); tmp.setInt(6, age); tmp.setString(7, wt); tmp.setString(8, addr); tmp.setString(9, cno); tmp.setString(10, dnm); tmp.setString(11, sym); tmp.setString(12, dig); tmp.setInt(13, fee); tmp.setString(14, bg); tmp.setString(16, s); tmp.setBinaryStream(15, (InputStream) fis, (int) (x.length())); tmp.executeUpdate(); } catch (Exception ee) { System.out.println("mod123 " + ee); } }
public ItensVendidos() { super("Itens Vendidos"); JButton imprimir; final DefaultTableModel modelo = new DefaultTableModel(); // constrói a tabela JTable tabela = new JTable(modelo); // Cria duas colunas modelo.addColumn("Código"); modelo.addColumn("Vendedor"); modelo.addColumn("Produto"); modelo.addColumn("Quantidade"); imprimir = new JButton("Imprimir"); imprimir.setBounds(100, 450, 30, 24); // exibe os dados da tabela MySQL // Conexao banco = new Conexao(); String retorno = "erro"; try { // Connection ExConn = (Connection) banco.abrirBDconn(); Class.forName("com.mysql.jdbc.Driver"); Connection conexao = DriverManager.getConnection("jdbc:mysql://localhost/banco", "root", ""); Statement stmt = conexao.createStatement(); // procedimentos para obter os dados de uma tabela String query = "SELECT * FROM venda2"; ResultSet rs = stmt.executeQuery(query); while (rs.next()) { int id = rs.getInt("idVendedor"); String nome = rs.getString("nomeVendedor"); String produto = rs.getString("produto"); int qtd = rs.getInt("qtd"); modelo.addRow(new Object[] {new Integer(id), nome, produto, new Integer(qtd)}); } // fim procedimento para obter os dados } catch (SQLException ex) { System.out.println("SQLException: " + ex.getMessage()); System.out.println("SQLState: " + ex.getSQLState()); System.out.println("VendorError: " + ex.getErrorCode()); } catch (Exception e) { System.out.println("Problemas ao tentar conectar com o banco de dados"); } // fim MySQL tabela.setPreferredScrollableViewportSize(new Dimension(350, 50)); Container c = getContentPane(); c.setLayout(new FlowLayout()); JScrollPane scrollPane = new JScrollPane(tabela); c.add(scrollPane); c.add(imprimir); setSize(400, 300); setVisible(true); }
public RAJ5(String sAno) { int files; int cols; String query11 = "drop table if exists tempAJ5 "; String query22 = "drop table if exists tempAJ5a "; String query1 = "create table tempAJ5 "; query1 += "select ano,descripcion as oficina,materia,sum(num) as num "; query1 += "from asist_juridica,oficinas "; query1 += "where (ano=\"" + sAno + "\") and (asist_juridica.oficina=oficinas.codigo) "; query1 += "group by materia,oficina "; query1 += "order by materia"; String query2 = "CREATE TABLE `tempAJ5a` ( "; query2 += "`ano` char(4) default '0', "; query2 += "`materia` char(15) default '0', "; query2 += "`of_benassal` int(3) unsigned default '0', "; query2 += "`of_montanejos` int(3) unsigned default '0', "; query2 += "`of_morella` int(3) unsigned default '0', "; query2 += "`of_onda` int(3) unsigned default '0', "; query2 += "`of_segorbe` int(3) unsigned default '0', "; query2 += "`of_traiguera` int(3) unsigned default '0'"; query2 += ") "; String query3 = "Select * from tipo_asist_jur"; String query4 = "select * from tempAJ5"; try { Connection con = this.getConnection(); Statement sentencia11 = con.createStatement(); sentencia11.execute(query11); sentencia11.close(); Statement sentencia22 = con.createStatement(); sentencia22.execute(query22); sentencia22.close(); Statement sentencia1 = con.createStatement(); sentencia1.execute(query1); sentencia1.close(); Statement sentencia2 = con.createStatement(); sentencia2.execute(query2); sentencia2.close(); Statement sentencia3 = con.createStatement(); ResultSet rs3 = sentencia3.executeQuery(query3); Statement sentencia4 = con.createStatement(); ResultSet rs4 = sentencia4.executeQuery(query4); String query5 = ""; String materia = ""; int quantitat, quantitat1, quantitat2, quantitat3, quantitat4, quantitat5, quantitat6; while (rs3.next()) { materia = rs3.getString("abreviatura"); quantitat = 0; quantitat1 = 0; quantitat2 = 0; quantitat3 = 0; quantitat4 = 0; quantitat5 = 0; quantitat6 = 0; while (rs4.next()) { if (materia.equals(rs4.getString("materia"))) { quantitat = rs4.getInt("num"); if (rs4.getString("oficina").equals("OF. BENASAL")) { quantitat1 = quantitat; } else if (rs4.getString("oficina").equals("OF. MONTANEJOS")) { quantitat2 = quantitat; } else if (rs4.getString("oficina").equals("OF. MORELLA")) { quantitat3 = quantitat; } else if (rs4.getString("oficina").equals("OF. ONDA")) { quantitat4 = quantitat; } else if (rs4.getString("oficina").equals("OF. SEGORBE")) { quantitat5 = quantitat; } else if (rs4.getString("oficina").equals("OF. TRAIGUERA")) { quantitat6 = quantitat; } } } query5 = "insert into tempAJ5a values (\"" + sAno + "\",\"" + materia + "\"," + quantitat1 + "," + quantitat2 + "," + quantitat3 + "," + quantitat4 + "," + quantitat5 + "," + quantitat6 + ")"; Statement sentencia5 = con.createStatement(); sentencia5.execute(query5); sentencia5.close(); rs4.beforeFirst(); } sentencia3.close(); sentencia4.close(); // select final per a formar la matriu AbstractTableModel String query6 = "select *,sum(of_traiguera+of_benassal+of_morella+of_onda+of_segorbe+of_montanejos)as total "; query6 += "from tempAJ5a "; query6 += "group by materia"; Statement sentencia6 = con.createStatement(); ResultSet rs6 = sentencia6.executeQuery(query6); ResultSetMetaData rsmd6 = rs6.getMetaData(); files = 0; while (rs6.next()) { files++; } cols = rsmd6.getColumnCount(); f = new Object[files][cols]; c = new Object[cols]; for (int i = 0; i < cols; i++) { c[i] = rsmd6.getColumnName(i + 1); } int x = 0; rs6.beforeFirst(); while (rs6.next()) { for (int y = 0; y < cols; y++) { if ((y >= 2) && (y <= 8)) { f[x][y] = new Integer(rs6.getString(y + 1)); } else { f[x][y] = rs6.getString(y + 1); } } x++; } sentencia6.close(); con.close(); } catch (SQLException e) { System.err.println("Error en la base de dades: " + e); } }