Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. public static Connection createConnection() {
  2. String server="DESKTOP-FKIO7OD\\SQLEXPRESS";
  3. int port=1433;
  4. String user="sa";
  5. String password="272627abc";
  6. String database="Bank" ;
  7. String jdbcurl;
  8. Connection con=null;
  9. try {
  10. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  11.  
  12. }catch(ClassNotFoundException e){
  13. e.printStackTrace();
  14. }
  15. jdbcurl = "jdbc:sqlserver://" + server + ":" + port + ";user=" + user
  16. + ";password=" + password + ";databasename=" + database + "";
  17. try {
  18.  
  19. con=DriverManager.getConnection( jdbcurl);;
  20. System.out.println("Worked");
  21. PreparedStatement P1=con.prepareStatement("insert into CUSTOMER values(?,?,?) ");
  22. PreparedStatement P=con.prepareStatement("Select * from CUSTOMER");
  23. ResultSet rs=P.executeQuery();
  24. P1.setInt(1,3);
  25. P1.setString(2,"sara");
  26. P1.setString(3,"saida");
  27. // P1.executeUpdate();
  28. //ResultSet rs1=P1.executeQuery();
  29.  
  30. while(rs.next())
  31. System.out.println("ID: "+rs.getInt("CID")+ " Name:"+rs.getString("CNAME")+" CITY:"+rs.getString("CCITY"));
  32.  
  33. } catch (SQLException e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. }
  37. return con;
  38.  
  39. }
  40. public static void main(String[]args){
  41. createConnection();
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement