Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package JDBC;
- import java.sql.*;
- public class ForDataBase {
- private Connection con;
- public ForDataBase(){//constructor
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db1","root","12345678");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void insertUpdateDelete(String sql){
- try{
- Statement st = con.createStatement();
- st.executeUpdate(sql);
- st.close();
- } catch (SQLException e){
- e.printStackTrace();
- }
- }
- public String [][] select(String sql){
- String result [][]=null;
- try{
- Statement st = con.createStatement(
- ResultSet.TYPE_SCROLL_INSENSITIVE,
- ResultSet.CONCUR_UPDATABLE);
- ResultSet res = st.executeQuery(sql);
- int m=res.getMetaData().getColumnCount();
- res.last();
- int n=res.getRow();
- result=new String[n][m];
- res.beforeFirst();
- int i=0;
- while (res.next())
- {
- for(int j=0;j<m;j++)
- result[i][j]=res.getString(j+1);
- i++;
- }
- res.close();
- st.close();
- } catch (SQLException e){
- e.printStackTrace();
- }
- return result;
- }
- public String [] getColNames(String sql){
- String result []=null;
- try{
- Statement st = con.createStatement();
- ResultSet res = st.executeQuery(sql);
- int m=res.getMetaData().getColumnCount();
- result=new String[m];
- for(int i=0;i<m;i++)
- result[i]=res.getMetaData().getColumnName(i+1);
- } catch (SQLException e){
- e.printStackTrace();
- }
- return result;
- }
- public static void main(String args[]){
- ForDataBase db=new ForDataBase();
- db.insertUpdateDelete("insert into clients values('Alla','1234')");
- String title []=db.getColNames("select * from clients");
- for(int i=0;i<title.length;i++){
- System.out.print (title[i]+" ");
- }
- System.out.println("");
- String mar [][]=db.select("select * from clients");
- for(int i=0;i<mar.length;i++)
- {
- for(int j=0;j<mar[i].length;j++)
- System.out.print (mar[i][j]+" ");
- System.out.println("");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement