Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.Properties;
- import java.sql.*;
- public class Register {
- public static void main(String[] args) {
- Connection con = null;
- String url = "jdbc:mysql://localhost:3306/";
- String db = "studentdb";
- String driver = "com.mysql.jdbc.Driver";
- String user = "root";
- String pass = "root";
- try {
- //conn3 = DriverManager.getConnection(url3, info);
- Class.forName(driver);
- con = DriverManager.getConnection(url+db, user, pass);
- if (con != null) {
- System.out.println("Connected to the database studentdb");
- System.out.println("Con-> "+con);
- try{
- //con.setAutoCommit(false);
- // the mysql insert statement
- String queryInsert = " insert into student (name, email, pass)"
- + " values (?, ?, ?)";
- // execute the preparedstatement
- //preparedStmt.execute();
- //con.commit();
- //con.setAutoCommit(true);
- // our SQL SELECT query.
- // if you only need a few columns, specify them by name instead of using "*"
- String query = "SELECT * FROM student";
- // create the java statement
- PreparedStatement st = con.prepareStatement(query);
- System.out.println("St-> "+st);
- // execute the query, and get a java resultset
- ResultSet rs = st.executeQuery();
- //System.out.println("-> "+rs.next());
- // iterate through the java resultset
- while (rs.next())
- {
- String name = rs.getString("name");
- String email = rs.getString("email");
- String password = rs.getString("pass");
- // print the results
- System.out.format("%s, %s, %sn", name, email, password);
- }
- st.close();
- }
- catch(Exception e){
- System.out.println("error came-->"+e.getMessage());
- }
- }
- } catch (Exception ex) {
- System.out.println("An error occurred. Maybe user/password is invalid");
- ex.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment