Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- final String JDBC_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
- final String DB_URL = "jdbc:sqlserver://localhost:1434;databaseName=UniplacBD;user=sa;password=123"; //
- Connection conn = null;
- Statement stmt = null;
- PreparedStatement pstmt = null;
- try {
- /**
- * ******** INICIO conectar ao banco *********
- */
- // Registra o driver JDBC e realiza a conexao
- Class.forName(JDBC_DRIVER);
- conn = DriverManager.getConnection(DB_URL);
- /**
- * ******** FIM conectar ao banco *********
- */
- /**
- * ******** INICIO exemplo insert por prepareStatement *********
- */
- // cria um preparedStatement
- pstmt = conn.prepareStatement("insert into fornecedor (razaoSocial, endereco, complemento, bairro, cidade, estado, cep, telefone, eMail, cnpj) values (?,?,?,?,?,?,?,?,?,?)");
- // preenche os valores
- pstmt.setString(1, razãosocial.getText());
- pstmt.setString(2, endereço.getText());
- pstmt.setString(3, complemento.getText());
- pstmt.setString(4, bairro.getText());
- pstmt.setString(5, cidade.getText());
- pstmt.setString(6, (String)estado.getSelectedItem());
- pstmt.setString(7, cep.getText());
- pstmt.setString(8, telefone.getText());
- pstmt.setString(9, email.getText());
- pstmt.setString(10, cnpj.getText());
- // executa e fecha
- pstmt.execute();
- pstmt.close();
- /**
- * ******** FIM exemplo insert por prepareStatement *********
- */
- //fecha a conexão com o banco de dados
- conn.close();
- } catch (SQLException se) {
- se.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (stmt != null) {
- stmt.close();
- }
- } catch (SQLException se2) {
- }
- try {
- if (conn != null) {
- conn.close();
- }
- } catch (SQLException se) {
- se.printStackTrace();
- }
- }
- // TODO add your handling code here:
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement