Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SOL_Server;
- import java.sql.Statement;
- import java.sql.Connection;
- import java.sql.DatabaseMetaData;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- public class DBExample2 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- List<String> list1 = GetAllProductName();
- for(String item : list1)
- {
- System.out.println(item);
- }
- InsertProduct("6","Product 6");
- list1 = GetAllProductName();
- for(String item : list1)
- {
- System.out.print(item);
- }
- }
- public static List<String> GetAllProductName()
- {
- Connection conn = null;
- ArrayList<String> prodcutlist = new ArrayList<String>();
- try{
- String dbURL = "jdbc:sqlserver://localhost\\SQLEXPRESS;DatabaseName=ExampleDatabase1";
- String user = "sa";
- String pass = "t3stt3st";
- conn = DriverManager.getConnection(dbURL, user , pass);
- if(conn != null) {
- Statement m_Statement = conn.createStatement();
- String query = "SELECT * FROM product_tbl";
- ResultSet m_ResultSet = m_Statement.executeQuery(query);
- while(m_ResultSet.next()){
- prodcutlist.add(m_ResultSet.getString(1)+","+m_ResultSet.getString(2));
- }
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- } finally {
- try {
- if(conn != null && !conn.isClosed()) {
- conn.close();
- }
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- return prodcutlist;
- }
- public static void InsertProduct(String id,String name)
- {
- Connection conn = null;
- ArrayList<String> prodcutlist = new ArrayList<String>();
- try{
- String dbURL = "jdbc:sqlserver://JAYAKRIT\\SQLEXPRESS;DatabaseName=happyWithDatabase ";
- String user = "sa";
- String pass = "t3stt3st";
- conn = DriverManager.getConnection(dbURL , user , pass);
- if(conn != null){
- Statement m_Statement = conn.createStatement();
- String query = "Insert into product_tbl (p) values('"+id+"','"+name+"') ";
- m_Statement.execute(query);
- }
- } catch (SQLException ex){
- ex.printStackTrace();
- } finally {
- try {
- if(conn != null && !conn.isClosed()){
- conn.close();
- }
- } catch (SQLException ex){
- ex.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement