TPBAFK TPBAFK - 5 months ago 21
AngularJS Question

how to read data from database using servlet?

i want to read data from database using servlet in netbeans and i'm beginner and i couldn't find any tutorial, actually there are some tutorials but complicated and i confused, i tried this - this - And this tutorials and the tutorials giving errors or doesn't execute querys i just want to do a java web app using servlet,database and netbeans (nice to have angularjs) is there any tutorial for beginner

Answer Source
Here you will get the basic idea about how it will work.
**JSP** page

    <body>
        <head>
           <form action="servlet1" action="post">
              <input type="text" name="input1"/>
           </form>
       </head>
    </body>`


Using the `input` tag, you can collect data from the user. 

In **Servlet**,inside doPost method,

`        String input = request.getParameter("input1");
         DatabaseConnection connection = new DatabaseConnection();
         String query = "select * from tableName where columnName='"+input+"'";
         PreparedStatement ps = connection.prepareStatement(query);
         ResultSet rs = ps.executeQuery();
         if(rs.next()){
              String firstColVal = rs.getString("firstColName");
          }
`
This will collect data from each column *.
For **database connection** create DatabaseConnection class,and write, to connect with database.

Connection  connection=driverManager.getConnection("jdbc:mysql://localhost:portNumber/databaseName", "userName", "password");





In Netbeans, right click on project->select propeties->click libraries->add libraries->select import ->MYSQL JDBC Driver->click add library.
   Hope this sample will help you.