Would you like to make this site your homepage? It's fast and easy...
Yes, Please make this my home page!
package a.b.c;
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class MYDBCon{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306";
// static final String DB_URL = "jdbc:mysql://localhost/EMP";
// Database credentials
static final String USER = "root";
static final String PASS = "SAM$1000";
private static final String DB_CONNECTION = "jdbc:mysql://localhost:3306";
// Connection con = null;
public static void Query(){
Connection con1,con= null;
Statement stmt,stmt1= null;
try{
Class.forName(JDBC_DRIVER);
// con=DriverManager.getConnection(DB_URL,USER,PASS);
// con1=DriverManager.getConnection(DB_URL,USER,PASS);
con=getDBConnection();
con1=getDBConnection();
//here sonoo is database name, root is username and password
stmt=con.createStatement();
////////////////////////////// INPUT ////////////
String userid=JOptionPane.showInputDialog(null,"Enter Your User ID(Number only):");
String userName=JOptionPane.showInputDialog(null,"Enter Your NameD:");
int myid=Integer.parseInt(userid);
///////////////////////
String mysql1="insert into student.st(id,fname)values("+myid+","+"'"+userName+"'"+")";
stmt1=con1.createStatement();
// JOptionPane.showMessageDialog(null,mysql1);
// stmt1.executeUpdate("insert into student.st(id,fname)values(900,'Ansar')");
stmt1.executeUpdate(mysql1);
ResultSet rs=stmt.executeQuery("select * from student.st");
while(rs.next())
System.out.println(rs.getInt("id")+" "+rs.getString("fname"));
// con.close();
}
catch(Exception e){
System.out.println(e);
}
}
///////////////////////////////////////
private static Connection getDBConnection() {
Connection dbConnection = null;
Statement statement = null;
try {
Class.forName(JDBC_DRIVER);
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
dbConnection = DriverManager.getConnection(
DB_CONNECTION, USER,PASS);
return dbConnection;
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return dbConnection;
}
}